Geeks With Blogs

Michael Freidgeim's Blog MS .Net Development

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 on Monday, May 28, 2007 5:31 PM | Back to top


Comments on this post: WaitAll to support calls from WindowsForms

# re: WaitAll to support calls from WindowsForms
Requesting Gravatar...
Yo Rock.. just what I was looking for
Left by j on Jan 02, 2008 7:28 AM

# re: WaitAll to support calls from WindowsForms
Requesting Gravatar...
possible to have the equivalent in vb.net? Tahnk you for your time and effort
Left by l a on Sep 01, 2009 9:06 AM

Your comment:
 (will show your gravatar)


Copyright © Michael Freidgeim | Powered by: GeeksWithBlogs.net | Join free