none
Async Design Pattern RRS feed

  • Question

  • Hi All,

    I am trying to take advantage of async. pattern to do some parallel work while waiting to get results from WCF calls. I am contemplating whether I am employing the correct approach. My objective is to leverage async pattern and not put additional stress on thread pool at the expense of doing parallel work.

    Here is how my code is organized:

    Client class --> assembly ---> WCF

    I have a client class that uses an assembly which inturn invokes a WCF service to get data which client uses them eventually. If at all possible, I do not want to make any changes to assembly and WCF services. Here is how the code is organized in client class:

    //this should be done asynchronously (operation 1) List<ContactDTO> listTemp = GetClientUsers(cID); //this can also be done asynchronously (operation 2) List<ContactDTO> listTemp1 = GetClientSuppliers(cID); //this should be done as a parallel work DoLengthyOperation1(); DoLengthyOperation2(); DoLengthyOperation3(); //end parallel work //use results from async calls to build UI components

    Since I do not want to change the assembly and WCF service, is my only option to use delegates and callbacks to introduce asynchronous operations? Delegates and callbacks would defeat the entire purpose since they use threadpool threads....correct?

    Thanks for any suggestions.

    Friday, December 20, 2013 8:59 PM

Answers

  • Hi,

    >>is my only option to use delegates and callbacks to introduce asynchronous operations?

    Yes, I am afraid that you have to use the delegates and callbacks to introduce asynchronous operations.

    >>Delegates and callbacks would defeat the entire purpose since they use threadpool threads....correct?

    Yes, the asynchronous delegates are executed using a thread from the thread pool. This reduces the overhead of manually creating a thread and disposing it. Threadpool threads have lesser overhead than the ones that you create manually and has to be disposed. So they should be faster compared to manual thread creation.

    Best Regards,
    Amy Peng


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Monday, December 23, 2013 6:04 AM
    Moderator
  • Hi,

    Yes, I think it is a good approach.

    >>What is the difference between AsyncWaitHandle.Wait and AsyncWaitHandle.WaitAll?

    The two methods allow you to wait on a windows events. WaitOne is non-static and waits on the event handle for that object. WaitAll and WaitAny are static class-wide methods that wait an a group of event handlers. WaitAll waits for all events to signal and WaitAny for a single event in the group.

    For more information, please try to check this similar thread:
    http://stackoverflow.com/questions/1816099/details-of-asyncwaithandle-waitone .

    >>Is one better over other here in this context where I have two async operations

    WaitOne blocks the current thread until the current WaitHandle receives a signal. And it is primarily used to synchronise asynchronous operations i.e one thread synchronising with another thread.


    >>
    Do I have to close WaitHandles?
    It is recommended that you should close the wait handle as soon as you are finished using it, by calling the WaitHandle.Close method. If you simply release all references to the wait handle, system resources are freed when garbage collection reclaims the wait handle, but garbage collection works more efficiently when disposable objects are explicitly closed or disposed.

    Best Regards,
    Amy Peng


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    Wednesday, January 1, 2014 9:15 AM
    Moderator

All replies

  • Hi,

    >>is my only option to use delegates and callbacks to introduce asynchronous operations?

    Yes, I am afraid that you have to use the delegates and callbacks to introduce asynchronous operations.

    >>Delegates and callbacks would defeat the entire purpose since they use threadpool threads....correct?

    Yes, the asynchronous delegates are executed using a thread from the thread pool. This reduces the overhead of manually creating a thread and disposing it. Threadpool threads have lesser overhead than the ones that you create manually and has to be disposed. So they should be faster compared to manual thread creation.

    Best Regards,
    Amy Peng


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Monday, December 23, 2013 6:04 AM
    Moderator
  • Thank you Amy.

    Here is what I came up with. Please suggest if this is a good approach with respect to introduction of async operations.

    // create the delegate for operation # 1
    GetClientDataAsyncStub caller = new GetClientDataAsyncStub(GetClientUsers);
    // initiate the async call for operation # 1
    IAsyncResult result = caller.BeginInvoke(cID, null, null);
    
    // create the delegate for operation # 2
    GetClientDataAsyncStub caller1 = new GetClientDataAsyncStub(GetClientSuppliers);
    // initiate the async call for operation # 2
    IAsyncResult result1 = caller1.BeginInvoke(cID, null, null);
    
    
    //lenghty synchronous operations are executed here in this block in parallel
    LenghtyOperation1();
    LengthOperation2();
    LengthOperation3();
    
    
    //block current thread until async operation # 1 is completed
    result.AsyncWaitHandle.WaitOne();
    List<CContact> list = caller.EndInvoke(result);
    ProcessUsers(list, tabTitle);
    
    //block current thread until async operation # 2 is completed
    result1.AsyncWaitHandle.WaitOne();
    List<CContact> listTemp1 = caller1.EndInvoke(result1);
    ProcessSuppliers(listTemp1);


    What is the difference between AsyncWaitHandle.Wait and AsyncWaitHandle.WaitAll? Is one better over other here in this context where I have two async operations?

    Do I have to close WaitHandles?




    • Edited by diffident Monday, December 23, 2013 6:05 PM
    Monday, December 23, 2013 6:03 PM
  • Hi,

    Yes, I think it is a good approach.

    >>What is the difference between AsyncWaitHandle.Wait and AsyncWaitHandle.WaitAll?

    The two methods allow you to wait on a windows events. WaitOne is non-static and waits on the event handle for that object. WaitAll and WaitAny are static class-wide methods that wait an a group of event handlers. WaitAll waits for all events to signal and WaitAny for a single event in the group.

    For more information, please try to check this similar thread:
    http://stackoverflow.com/questions/1816099/details-of-asyncwaithandle-waitone .

    >>Is one better over other here in this context where I have two async operations

    WaitOne blocks the current thread until the current WaitHandle receives a signal. And it is primarily used to synchronise asynchronous operations i.e one thread synchronising with another thread.


    >>
    Do I have to close WaitHandles?
    It is recommended that you should close the wait handle as soon as you are finished using it, by calling the WaitHandle.Close method. If you simply release all references to the wait handle, system resources are freed when garbage collection reclaims the wait handle, but garbage collection works more efficiently when disposable objects are explicitly closed or disposed.

    Best Regards,
    Amy Peng


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    Wednesday, January 1, 2014 9:15 AM
    Moderator
  • Thank you Amy.

    Here is what I came up with. Please suggest if this is a good approach with respect to introduction of async operations.

    // create the delegate for operation # 1
    GetClientDataAsyncStub caller = new GetClientDataAsyncStub(GetClientUsers);
    // initiate the async call for operation # 1
    IAsyncResult result = caller.BeginInvoke(cID, null, null);
    
    // create the delegate for operation # 2
    GetClientDataAsyncStub caller1 = new GetClientDataAsyncStub(GetClientSuppliers);
    // initiate the async call for operation # 2
    IAsyncResult result1 = caller1.BeginInvoke(cID, null, null);
    
    
    //lenghty synchronous operations are executed here in this block in parallel
    LenghtyOperation1();
    LengthOperation2();
    LengthOperation3();
    
    
    //block current thread until async operation # 1 is completed
    result.AsyncWaitHandle.WaitOne();
    List<CContact> list = caller.EndInvoke(result);
    ProcessUsers(list, tabTitle);
    
    //block current thread until async operation # 2 is completed
    result1.AsyncWaitHandle.WaitOne();
    List<CContact> listTemp1 = caller1.EndInvoke(result1);
    ProcessSuppliers(listTemp1);


    What is the difference between AsyncWaitHandle.Wait and AsyncWaitHandle.WaitAll? Is one better over other here in this context where I have two async operations?

    Do I have to close WaitHandles?





    Am I correct in thinking that the above code uses 3 threads from the thread pool - 2 threads to perform async operations and then the main thread that executes the code.
    Thursday, January 23, 2014 9:31 PM