none
How to make the Main thread to wait til end of all async callback method RRS feed

  • Question

  • Hi,

    I need litile help related in Async callback programing. my cenario is I am instansiating multiple 
    here is the code snipet.
    while (beginIndex < _meterList.Count - 1)
    {
         if (beginIndex + meterPerThread > _meterList.Count)
         {
               meterPerThread = (_meterList.Count - beginIndex);
         }
         _meterList.CopyTo(beginIndex, metersToPoll, 0,
         meterPerThread);
         AsyncMethodCaller asyncCaller = new AsyncMethodCaller(DoMeterPolling);
         IAsyncResult AsyncResult = asyncCaller.BeginInvoke(metersToPoll, new AsyncCallback(AsyncCallBackMethod), asyncCaller);
         IncreaseThreadCount();
         beginIndex = meterPerThread + beginIndex;
    
    }
    
    The callback Method is like

            private void AsyncCallBackMethod(IAsyncResult ar)
            {
                try
                {
                    AsyncMethodCaller caller = (AsyncMethodCaller)ar.AsyncState;
                    _mutex.WaitOne();
                    LogMessage(string.Format("call Back method called"),
                       _debugLogCategory, TraceEventType.Information, LoggerConstants.ePriority.Medium);
                    //caller.EndInvoke(ar);
                    MeterList mc = (MeterList)caller.EndInvoke(ar);
                    foreach (MeterCDO meter in mc)
                    {
                        _meterCollection.Add(meter);
                    }
                    _threadCount--;
                    if (_threadCount == 0)
                    {
                        //do rest off stuff.
                        RetryFailedMeters();
                        LoadBeginArDrValues();
                        InsertPollingDeatils();
                    }
                    _mutex.ReleaseMutex();
                    
    
    
                }
                catch (Exception ex)
                {
    
                }
    
            }
    My problem is that the main thread Exits as soon as it create all async callback so what hapen is that my all threads are running but main thread is no more.

    REQU: My requirment is that the main thread should remain idel until all the async callbaks are completed.

    I have searched for diffrent paterns to meet this requirment and most of them talk about making Main thread to sleep for some time and then check for number off threads and if there are still async threads are runing then sleep again.

    Is there any beter way of doing this? So that when all async callback ends they notify main thread insted of main thread pinging them.  

    I hope this is correct section to post this kind of quryes. If not  pleas let me know........
    • Edited by Sandeep8 Thursday, July 9, 2009 7:42 AM
    Thursday, July 9, 2009 7:32 AM

Answers

  • Each of the objects returned from BeginInvoke (IAsyncResult) contains wait handle, which can be retrieved from AsyncWaitHandler property. Just gather all these wait handles and use WaitHandle.WaitAll method in your main thread.
    Vitaliy Liptchinsky http://dotnetframeworkplanet.blogspot.com/
    Thursday, July 9, 2009 7:52 AM

All replies

  • Each of the objects returned from BeginInvoke (IAsyncResult) contains wait handle, which can be retrieved from AsyncWaitHandler property. Just gather all these wait handles and use WaitHandle.WaitAll method in your main thread.
    Vitaliy Liptchinsky http://dotnetframeworkplanet.blogspot.com/
    Thursday, July 9, 2009 7:52 AM
  • thanks for your reply... i will try your solution as soon as possible and then let you know..
    Monday, July 13, 2009 11:45 AM