Waiting for Asynchronous Delegates to Complete


Sometimes you want to execute multiple functions asynchronously, and then wait for all the functions to complete before continuing the execution of the program. For example, suppose your Web page is going to display an employee record. Displaying the record involves fetching multiple pieces of information from different database tables. Fetching the data from the multiple tables can be done at the same time using asynchronous delegates. But before presenting the information, you want to wait until all the delegates have fetched the particular data they were after.

To wait for several delegates to complete executing:

  1. Suppose you have started multiple delegate functions asynchronously and have stored the results in the variables : IAsyncResult ar1 , and IAsyncResult ar2 .

  2. Type System.Threading.WaitHandle[] handles = new System.Threading.WaitHandle[] {ar1.AsyncWaitHandle, ar2.AsyncWaitHandle} ; where handles is any name you want to assign to the array of WaitHandles .

  3. Type System.Threading.WaitHandle.WaitAll(handles); ( Figure 10.18 ).

    Figure 10.18 This looks complicated because it is. It basically tells the runtime to stop the program's main thread from running until the secondary threads are done executing. If we didn't do this, the main thread would get done before the secondary thread, and our page would be displayed before Task1 and Task2 finished.
     class Tasks {    public bool Task1(string desc)    {       return true;    }    public static bool Task2(string desc)    {       return true;    } } delegate bool TaskDel(string desc); public class WebForm1 : System.Web.UI.Page {    private void Page_Load(object sender,                 System.EventArgs e)    {       Tasks tsks = new Tasks();       TaskDel del1 =       new TaskDel(tsks.Task1);       TaskDel del2 =       new TaskDel(Tasks.Task2);       IAsyncResult ar1 =       del1.BeginInvoke(       "Calling Task1...",null,null);       IAsyncResult ar2 =       del2.BeginInvoke(       "Calling Task2...",null,null);  System.Threading.WaitHandle[]   handles =   new System.Threading.WaitHandle[]   {ar1.AsyncWaitHandle ,   ar2.AsyncWaitHandle };   System.Threading.WaitHandle.   WaitAll(handles);  } } 

graphics/tick.gif Tip

  • The WaitAll function also has overloaded methods that enable you to wait for a certain amount of time. This is useful if you want to prevent the program from freezing if, for example, one of the delegates fails to complete within a timely manner ( Figure 10.19 ).

    Figure 10.19 If you don't specify a timeout, the main thread will be paused indefinitely. If the functions take longer than the timeout period, the program will continue but the WaitAll function will return false. If the Tasks finish before the timeout period, then WaitAll returns true.
     class Tasks {    public bool Task1(string desc)    {       return true;    }    public static bool Task2(string desc)    {       return true;    } } delegate bool TaskDel(string desc); public class WebForm1 : System.Web.UI.Page {    private void Page_Load(object sender,                 System.EventArgs e)    {       Tasks tsks = new Tasks();       TaskDel del1 =       new TaskDel(tsks.Task1);       TaskDel del2 =       new TaskDel(Tasks.Task2);       IAsyncResult ar1 =       del1.BeginInvoke(       "Calling Task1...",null,null);       IAsyncResult ar2 =       del2.BeginInvoke(       "Calling Task2...",null,null);       System.Threading.WaitHandle[]       handles =       new System.Threading.WaitHandle[]       {ar1.AsyncWaitHandle ,       ar2.AsyncWaitHandle };  bool result =  System.Threading.WaitHandle.       WaitAll  (  handles,  2000,false);  } } 



C#
C# & VB.NET Conversion Pocket Reference
ISBN: 0596003196
EAN: 2147483647
Year: 2003
Pages: 198
Authors: Jose Mojica

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net