SanSYS

Untitled

May 6th, 2015
7,671
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.14 KB | None | 0 0
  1. using System;
  2. using System.Diagnostics;
  3. using System.Threading;
  4. using System.Threading.Tasks;
  5.  
  6. namespace TestAcync
  7. {
  8.     class Program
  9.     {
  10.         class Foo
  11.         {
  12.             public async void Do()
  13.             {
  14.                 Console.WriteLine("Enter your name: ");
  15.  
  16.                 string name = Console.ReadLine();
  17.  
  18.                 Stopwatch sw = Stopwatch.StartNew();
  19.  
  20.                 string s = await AsyncHello(name);
  21.  
  22.                 sw.Stop();
  23.  
  24.                 Console.WriteLine(string.Format("{0}, Ellapsed {1} ms", s, sw.ElapsedMilliseconds));
  25.  
  26.                 Console.ReadKey();
  27.             }
  28.  
  29.             public Task<string> AsyncHello(string name)
  30.             {
  31.                 var task = new Task<string>(() =>
  32.                 {
  33.                     Thread.Sleep(1000);
  34.  
  35.                     return string.Format("Hi, {0}", name);
  36.                 });
  37.  
  38.                 task.Start();
  39.  
  40.                 return task;
  41.             }
  42.         }
  43.  
  44.         static void Main(string[] args)
  45.         {
  46.             var foo = new Foo();
  47.  
  48.             foo.Do();
  49.  
  50.             Console.ReadKey();
  51.         }
  52.     }
  53. }
Advertisement
Add Comment
Please, Sign In to add comment