Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Threading;
- using System.Threading.Tasks;
- namespace MoreMethodExercises
- {
- class Program
- {
- static void Main()
- {
- // Start the task executing:
- Task<string> task = Task.Factory.StartNew<string>
- (() => DownloadString("http://www.linqpad.net"));
- // We can do other work here and it will execute in parallel:
- RunSomeOtherMethod();
- // When we need the task's return value, we query its Result property:
- // If it's still executing, the current thread will now block (wait)
- // until the task finishes:
- string result = task.Result;
- }
- private static void RunSomeOtherMethod()
- {
- for(int i = 0; i < 10; i++)
- {
- Console.WriteLine(i);
- Thread.Sleep(1000);
- }
- }
- static string DownloadString(string uri)
- {
- using (var wc = new System.Net.WebClient())
- return wc.DownloadString(uri);
- }
- }
- }
Add Comment
Please, Sign In to add comment