deadlock when using waitOne in a STA thread

Discussion in 'ASP .Net' started by Daniel Cuculescu, Jun 5, 2008.

  1. Hi,

    I have the problem described bellow with the calls to WaitHandle.WaitOne
    causing re-entrancy on main GUI thread (STA) of a .NET application and I
    would like to find out:
    - did anybody else run into the same problem?
    - is it a known issue? is there a fix for it?
    - is there a workaround for it?
    - if no fix/workaround I would like to find out the complete list of
    messages that are being handled (dispatched by ole32.dll OLE/COM message
    pump) when doing WaitOne on a STA thread.

    The problem is:

    I have 2 (or more) calls pending to execute on the main gui thread (I am
    actually doing Invoke on some control)
    the first call, at some point does WaitHandle.WaitOne.
    at this point, if a Windows message comes to the app (like
    WM_POPUPSYSTEMMENU - 0x0313 ) triggered by a right-click on the task bar
    icon of the app the following happens:
    - the app processes this message
    - the app starts processing other pending messages - like my second
    - at this point my app is deadlocked, because it is not supposed to
    enter the second call before finishing processing the first one.
    - also at this point the entire system is not behaving properly - for
    example I cannot bring up any window by clicking on the taskbar icon
    important note: if I would not do right click on the taskbar icon of my app,
    and just let it work in background, everything would be ok.

    After investigating this issue for a while, I found that WaitOne is not
    actually a blocking call when called from an STA thread.

    The only reference I found related to this matter in MSDN pages is here: .
    Quoting from that page:
    "WaitHandle..::.WaitOne,WaitHandle..::.WaitAny, WaitHandle..::.WaitAll,
    Monitor..::.Enter, Monitor..::.TryEnter, Thread..::.Join,
    GC..::.WaitForPendingFinalizers, and so on are all responsive to
    Thread..::.Interrupt and to Thread..::.Abort. Also, if your thread is in a
    single-threaded apartment, all these managed blocking operations will
    correctly pump messages in your apartment while your thread is blocked."

    If the apartment state is STA this function actually creates a message loop
    that is processing some events (for now I have been able to identify 2 of
    them (WM_POPUPSYSTEMMENU and WM_ACTIVATEAPP). The problem is that these
    messages triggers processing of other messages in the queue (like the
    registered messages used for invoke in .NET ).

    I have attached a simplified application to demonstrate the issue. In this
    app I have created a thread from where I am calling multiple times the same
    function which is suppose to execute on the GUI thread.
    I have put some console prints to show that it is executing another invoke
    on the main thread even if it is supposed to stay in the wait.

    Another thing, in my app the calls to some functions are not supposed to
    re-enter (and we are protecting them with the waitOnes) but if I modified
    the sample app (attached to this mail) to have a non-blocking re-entrancy ,
    I noticed if I right click on the taskbar icon while the first call is in
    waitOne, all the other calls will be executed before the first call ends -
    this is actually easy to explain since the other calls are not processed by
    the app message loop but they are processed by the message loop created by
    the WaitOne. however I don't think this should be the normal behavior - that
    is: when I invoke 3 calls on some thread, the order of execution should be
    the order of the invokes.

    waiting forward for your replies
    Daniel Cuculescu, Jun 5, 2008
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. pfurb

    Microsoft Help: IEExe.exe and STA

    pfurb, Jan 19, 2004, in forum: ASP .Net
    Jeff B.
    Jan 20, 2004
  2. =?Utf-8?B?UmF1bA==?=

    Re: Using STA API in a MTA enviroment

    =?Utf-8?B?UmF1bA==?=, Sep 17, 2004, in forum: ASP .Net
    Sep 17, 2004
  3. Replies:
  4. Marc ENGEL
    Marc ENGEL
    Jul 30, 2003
  5. Anthony Jones

    STA object in Session and Thread Affiliation

    Anthony Jones, Feb 6, 2006, in forum: ASP General
    Anthony Jones
    Feb 11, 2006

Share This Page