How can I call an async method in Main?

Your Main method can be simplified. For C# 7.1 and newer: static async Task Main(string[] args) { test t = new test(); await t.Go(); Console.WriteLine(“finished”); Console.ReadKey(); } For earlier versions of C#: static void Main(string[] args) { test t = new test(); t.Go().Wait(); Console.WriteLine(“finished”); Console.ReadKey(); } This is part of the beauty of the async … Read more

Await on a completed task same as task.Result?

There are already some good answers/comments here, but just to chime in… There are two reasons why I prefer await over Result (or Wait). The first is that the error handling is different; await does not wrap the exception in an AggregateException. Ideally, asynchronous code should never have to deal with AggregateException at all, unless … Read more

Should I worry about “This async method lacks ‘await’ operators and will run synchronously” warning

The async keyword is merely an implementation detail of a method; it isn’t part of the method signature. If a particular method implementation or override has nothing to await, then just omit the async keyword and return a completed task using Task.FromResult<TResult>: public Task<string> Foo() // public async Task<string> Foo() { // { Baz(); // … Read more

Difference between await and ContinueWith

Here’s the sequence of code snippets I recently used to illustrate the difference and various problems using async solves. Suppose you have some event handler in your GUI-based application that takes a lot of time, and so you’d like to make it asynchronous. Here’s the synchronous logic you start with: while (true) { string result … Read more

Run PHP Task Asynchronously

I’ve used the queuing approach, and it works well as you can defer that processing until your server load is idle, letting you manage your load quite effectively if you can partition off “tasks which aren’t urgent” easily. Rolling your own isn’t too tricky, here’s a few other options to check out: GearMan – this … Read more

How do I abort/cancel TPL Tasks?

You can’t. Tasks use background threads from the thread pool. Also canceling threads using the Abort method is not recommended. You may take a look at the following blog post which explains a proper way of canceling tasks using cancellation tokens. Here’s an example: class Program { static void Main() { var ts = new … Read more

Task continuation on UI thread

Call the continuation with TaskScheduler.FromCurrentSynchronizationContext(): Task UITask= task.ContinueWith(() => { this.TextBlock1.Text = “Complete”; }, TaskScheduler.FromCurrentSynchronizationContext()); This is suitable only if the current execution context is on the UI thread.

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)