Last active
August 29, 2015 14:18
-
-
Save games/594d4c72e2f8078433e0 to your computer and use it in GitHub Desktop.
a test for Task in C#
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System; | |
| using System.Collections.Generic; | |
| using System.Linq; | |
| using System.Text; | |
| using System.Threading; | |
| using System.Threading.Tasks; | |
| namespace ConsoleApplication1 { | |
| class Test { | |
| public async void F1() { | |
| Console.WriteLine("F1 > Thread-{0}", Thread.CurrentThread.ManagedThreadId); | |
| Thread.Sleep(2000); | |
| Console.WriteLine("F1 done"); | |
| } | |
| public async void F4() { | |
| Console.WriteLine("F4 > before await Thread-{0}", Thread.CurrentThread.ManagedThreadId); | |
| await Task.Delay(2000); | |
| Console.WriteLine("F4 > after await Thread-{0}", Thread.CurrentThread.ManagedThreadId); | |
| } | |
| public Task F2() { | |
| Console.WriteLine("F2 outside of Task > Thread-{0}", Thread.CurrentThread.ManagedThreadId); | |
| return Task.Run(() => { | |
| Console.WriteLine("F2 inside of Task > Thread-{0}", Thread.CurrentThread.ManagedThreadId); | |
| Thread.Sleep(2000); | |
| Console.WriteLine("F2 done"); | |
| }); | |
| } | |
| public async Task<int> F3() { | |
| Console.WriteLine("F3 before await > Thread-{0}", Thread.CurrentThread.ManagedThreadId); | |
| await Task.Delay(2000); | |
| Console.WriteLine("F3 after await > Thread-{0}", Thread.CurrentThread.ManagedThreadId); | |
| return 1; | |
| } | |
| } | |
| class Program { | |
| static void Main(string[] args) { | |
| while (true) { | |
| Console.WriteLine("+++++++++++++++++++++++++++++++"); | |
| Console.WriteLine("UI Thread-{0}", Thread.CurrentThread.ManagedThreadId); | |
| var t = new Test(); | |
| Console.WriteLine("F1 starting"); | |
| t.F1(); | |
| Console.WriteLine("F2 starting"); | |
| t.F2(); | |
| Console.WriteLine("F3 starting"); | |
| t.F3(); | |
| Console.WriteLine("F4 starting"); | |
| t.F4(); | |
| Console.WriteLine("all done ??"); | |
| if (Console.ReadLine() == "q") { | |
| break; | |
| } | |
| } | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment