Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Program
- {
- static void DoIt(string entry)
- {
- Console.WriteLine("Message: " + entry);
- }
- static async void DoIt2(string entry)
- {
- await Task.Yield();
- Console.WriteLine("Message2: " + entry);
- }
- static void Main(string[] args)
- {
- // old way
- Action<string> async = DoIt;
- async.BeginInvoke("Test", ar => { async.EndInvoke(ar); ar.AsyncWaitHandle.Close(); }, null);
- Console.WriteLine("old-way main thread invoker finished");
- // new way
- DoIt2("Test2");
- Console.WriteLine("new-way main thread invoker finished");
- Console.ReadLine();
- }
- }
- Task.Run(A);
- static async void FireAndForget(this Task task)
- {
- try
- {
- await task;
- }
- catch (Exception e)
- {
- // log errors
- }
- }
- MyTaskAsyncMethod().FireAndForget();
- Task.Factory.StartNew(() => DoIt2("Test2"))
- public async void Method()
- {
- //Do UI stuff
- await SomeTaskAsync();
- //Do more UI stuff (as if called via Invoke from a thread)
- var nextTask = NextTaskAsync();
- //Do UI stuff while task is running (as if called via BeginInvoke from a thread)
- await nextTask;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement