How to create and start new Task
private void LongTaskNoParam()
{
// do something here
}
Task task1 = new Task(LongTaskNoParam);
task1.Start();
More info and samples on: www.devarchweb.net
How to create and start new Task with factoryTask task2 = Task.Factory.StartNew(LongTaskNoParam);
More info and samples on: www.devarchweb.net
How to pass parametr to a Taskprivate void LongTaskParam(object myParam)
{
// do something here
}
Task task3 = Task.Factory.StartNew(LongTaskParam, 3000);
More info and samples on: www.devarchweb.net
How to wait for a Tasktask3.Wait();
More info and samples on: www.devarchweb.net
How to run task synchronously on the same threadTask task4 = new Task(LongTaskNoParam);
task4.RunSynchronously();
More info and samples on: www.devarchweb.net
Run async, return a value and wait for completionpublic async Task<string> RunsAsynchronously(int sleepTime)
{
Log("1");
await Task.Delay(sleepTime);
Log("2"); // start + sleep time
return " Finished Async";
}
private async void button1_Click(object sender, EventArgs e)
{
string s = await RunsAsynchronously(3000);
Log("3"); // start + sleep time
}
More info and samples on: www.devarchweb.net
What will happen when async worker method is not called with awaitpublic async Task<string> RunsAsynchronously(int sleepTime)
{
Log("2");
await Task.Delay(sleepTime);
Log("3");
return " Finished Async";
}
private async void button1_Click(object sender, EventArgs e)
{
RunsAsynchronously(3000); // cannot assign result "string s = " without "await"
Log("1"); // start RunsAsynchronously() and continues
}
More info and samples on: www.devarchweb.net
What will happen when async worker method is called with await, but worker code does not awaitpublic async Task<string> RunsAsynchronously(int sleepTime)
{
Log("1");
Task.Delay(sleepTime); // will not wait here without "await"
Log("2"); // Log("1") + 0.01sec
return " Finished Async";
}
private async void button1_Click(object sender, EventArgs e)
{
string s = await RunsAsynchronously(3000);
Log("3"); // Log("2") + 0.01s time
}
More info and samples on: www.devarchweb.net
When method with async signature will not run asynchronouslypublic async Task<string> WillNotRunAsync()
{
Thread.Sleep(2000); // simulates long running method
return "Did not run async";
}
Task<string> result = await WillNotRunAsync();
More info and samples on: www.devarchweb.net
How to call a method without async signature asynchronously from an async methodprivate string LongTaskWithResult()
{
Thread.Sleep(5000);
return "LongTaskWithResult";
}
private async Task<string> LongTaskWithResultAsync()
{
string s = await Task.Factory.StartNew(() => LongTaskWithResult());
return s;
}
...
string s = await LongTaskWithResultAsync();
string s = await Task.Factory.StartNew(() => LongTaskWithResult());
More info and samples on: www.devarchweb.net
How to call a method with async signature asynchronously from a non async methodstring s;
Task.Factory.StartNew(async () =>
{
s = await LongTaskWithResultAsync();
// here the code waits
DoSomething();
});
// here the code does NOT wait
More info and samples on: www.devarchweb.net
How to update UI from non UI threadusing System.Threading;
....
SynchronizationContext m_syncContext;
SendOrPostCallback m_UIUpdateMethod;
public MyForm()
{
m_syncContext = SynchronizationContext.Current; // stores main thread context
m_UIUpdateMethod = UIUpdateMethod;
}
private void UIUpdateMethod(object state)
{
textBox1.Text += (string)state;
}
public async Task MyAsynchronousMethod()
{
m_syncContext.Post(m_UIUpdateMethod, "Hello from MyAsynchronousMethod()");
}
More info and samples on: www.devarchweb.net
How to cancel an async callprivate void LongTask(CancellationToken ct)
{
for (int i=0; i < 100; i++)
{
if (ct.IsCancellationRequested)
{
return;
}
else
{
// Do something
}
}
}
CancellationTokenSource cts = new CancellationTokenSource();
CancellationToken ct = cts.Token;
Task.Factory.StartNew(() => LongTask(ct), ct);
...
cts.Cancel(); // cancel when needed