WaitAll to support calls from WindowsForms

We have multi-threading application that uses WaitHandle.WaitAll methods. It works fine, when called from ASP.NET, but when it is called from WinForms test harness, it causes the error "WaitAll for multiple handles on an STA thread is not supported.". I've included the workaround and moved code to helper function.


However I needed to call the function with different parameters List<ManualResetEvent> and List<WaitHandle> . Even if ManualResetEvent is derived from WaitHandle, there is no implicit casting. Threads

Question regarding Generics and Inheritance and Casting List<> of derived class to List<> of base class? pointed me to ConvertAll method of List<> generic. And the article Introduction to anonymous methods in C#  helped me to use anonymous delegate to reduce code. 
The final helper class is shown below" 

 

public class ThreadingHelper

    {

        public static bool WaitAll(List<ManualResetEvent> eventList , int millisecondsTimeout)

        {//Casting List<> of derived class to List<> of base class? see http://www.thescripts.com/forum/thread276346.html

            //new Converter<ManualResetEvent, WaitHandle>(ConvertDelegate));

 

            //and http://www.theserverside.net/tt/articles/showarticle.tss?id=AnonymousMethods

            List<WaitHandle> waitHandles = eventList.ConvertAll<WaitHandle>( 

                    delegate(ManualResetEvent ev) { return (WaitHandle)ev; });

           return WaitAll( waitHandles, millisecondsTimeout);

        }

        public static bool WaitAll(List<WaitHandle> waitHandles, int millisecondsTimeout)

        {

            bool bRet=true;

            //to avoid "WaitAll for multiple handles on an STA thread is not supported." SEE http://www.devnewsgroups.net/group/microsoft.public.dotnet.framework/topic28609.aspx

            //alternative solutions described in http://www.issociate.de/board/post/250510/WaitAll_for_multiple_handles_on_an_STA_thread_is_not_supported.html

            if (Thread.CurrentThread.GetApartmentState() == ApartmentState.STA)

            {//usually happens when tested from TestHarness or NUnit

                foreach (WaitHandle handle in waitHandles)

                {

                    bRet |=handle.WaitOne();

                }

            }

            else

            {

                if (millisecondsTimeout > 0)

                {

                    bRet=WaitHandle.WaitAll(waitHandles.ToArray(), millisecondsTimeout, false);//

                }

                else

                {

                    bRet=WaitHandle.WaitAll(waitHandles.ToArray());

                   

                }

            }

            return bRet;

        }

 

    }

posted @ Monday, May 28, 2007 5:31 PM
Print

Comments on this entry:

# re: WaitAll to support calls from WindowsForms

Left by j at 1/2/2008 7:28 AM
Gravatar
Yo Rock.. just what I was looking for

# re: WaitAll to support calls from WindowsForms

Left by l a at 9/1/2009 9:06 AM
Gravatar
possible to have the equivalent in vb.net? Tahnk you for your time and effort

Your comment:



(not displayed)

 
 
 
 

Live Comment Preview:

 
«November»
SunMonTueWedThuFriSat
2627282930311
2345678
9101112131415
16171819202122
23242526272829
30123456