Threads, tasks, and synchronized learning notes (1). Synchronize learning notes

Source: Internet
Author: User

Threads, tasks, and synchronized learning notes (1). Synchronize learning notes

1. A simple way to create a thread is to define a delegate and call it asynchronously.

 1 using System; 2 using System.Threading; 3  4 delegate int WaitAWhileDelegate(int data, int ms); 5  6 class Program 7 { 8     static void Main(string[] args) 9     {10         WaitAWhileDelegate d = WaitAWhile;11         int data = 1;12         int ms = 3000;13         IAsyncResult a = d.BeginInvoke(data, ms, null, null);14         while (!a.IsCompleted)15         {16             Console.Write("*");17             Thread.Sleep(100);18         }19         int result = d.EndInvoke(a);20         Console.WriteLine("\nresult: {0}", result);21     }22 23     static int WaitAWhile(int data, int ms)24     {25         Console.WriteLine("WaitAWhile started.");26         Thread.Sleep(ms);27         Console.WriteLine("WaitAWhile completed.");28         return ++data;29     }30 }

Add the parameter value of the Sleep method of Line 1 in the Code. The number of asterisks output is reduced, and vice versa.

Running result:

2. IAsyncResult has an attribute named AsyncWaitHandle, which belongs to the WaitHandle class. The WaitOne method of this class will "take a timeout time as the first optional parameter, in which you can define the maximum waiting time ".

 1 using System; 2 using System.Threading; 3  4 class Program 5 { 6     delegate int WaitAWhileDelegate(int data, int ms); 7  8     static void Main(string[] args) 9     {10         WaitAWhileDelegate d = new WaitAWhileDelegate(WaitAWhile);11         int data = 1;12         int ms = 3000;13         IAsyncResult a = d.BeginInvoke(data, ms, null, null);14         while (true)15         {16             Console.Write("*");17             if (a.AsyncWaitHandle.WaitOne(50, false))18             {19                 Console.WriteLine("Can get the result now.");20                 break;21             }22         }23         int result = d.EndInvoke(a);24         Console.WriteLine("\nresult: {0}", result);25     }26 27     static int WaitAWhile(int data, int ms)28     {29         Console.WriteLine("WaitAWhile started.");30         Thread.Sleep(ms);31         Console.WriteLine("WaitAWhile completed.");32         return ++data;33     }34 }

Running result:

3. The third way to wait for the result is asynchronous callback. IAsyncResult also has an attribute named AsyncState. This attribute can be forcibly converted to its delegate.

 1 using System; 2 using System.Threading; 3  4 class Program 5 { 6     delegate int WaitAWhileDelegate(int data, int ms); 7  8     static void Main(string[] args) 9     {10         WaitAWhileDelegate d = new WaitAWhileDelegate(WaitAWhile);11         d.BeginInvoke(1, 1000, WaitAWhileComplete, d);12         for (int i = 0; i < 100; i++)13         {14             Console.Write("*");15             Thread.Sleep(50);16         }17     }18 19     static int WaitAWhile(int data, int ms)20     {21         Console.WriteLine("WaitAWhile started.");22         Thread.Sleep(ms);23         Console.WriteLine("WaitAWhile completed.");24         return ++data;25     }26 27     static void WaitAWhileComplete(IAsyncResult a)28     {29         if (a == null)30         {31             throw new ArgumentNullException("a");32         }33         WaitAWhileDelegate d = a.AsyncState as WaitAWhileDelegate;34         if (d == null)35         {36             throw new Exception("a.AsyncState can not convert to WaitAWhileDelegate");37         }38         int result = d.EndInvoke(a);39         Console.WriteLine("\nresult: {0}", result);40     }41 }

Note: The 11th parameter d in the BeginInvoke method of the 4th line Code cannot be null any more. Otherwise, the callback cannot be implemented, that is, the print output function of the 39th line in the WaitAWhileComplete function cannot be executed.

Running result:

4. Lamda expressions can be used instead of delegation.

 1 using System; 2 using System.Threading; 3  4 class Program 5 { 6     delegate int WaitAWhileDelegate(int data, int ms); 7  8     static void Main(string[] args) 9     {10         WaitAWhileDelegate d = new WaitAWhileDelegate(WaitAWhile);11         d.BeginInvoke(1, 1000, a => { int result = d.EndInvoke(a); Console.WriteLine("\nresult: {0}", result); }, d);12         for (int i = 0; i < 100; i++)13         {14             Console.Write("*");15             Thread.Sleep(50);16         }17     }18 19     static int WaitAWhile(int data, int ms)20     {21         Console.WriteLine("WaitAWhile started.");22         Thread.Sleep(ms);23         Console.WriteLine("WaitAWhile completed.");24         return ++data;25     }26 }

Running result:

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.