async call to ActiveX is still blocking UI

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Marwan

    async call to ActiveX is still blocking UI

    Hello

    I am using asynchronous delegates to make a call to a COM ActiveX object, but even though the call occurs on a separate thread, my UI is still blocking.

    If i put the thread to sleep in my delegate call, the application is well behaved (no UI freeze), but the call to the com object causes the UI to lock up

    Do I have to manage calls to an ActiveX object differently than using the BeginInvoke and a callback

    A sample of the code I am using
    /// <summary
    // Connect to the devic
    /// </summary
    /// <returns></returns
    public bool BeginConnect(

    bool bRetVal = false
    IAsyncResult ar
    int threadId

    // Create the delegate
    AsyncDelegate dlgt = new AsyncDelegate(m _ad.AutoConnect )

    threadId = AppDomain.GetCu rrentThreadId()
    Console.WriteLi ne("In BeginConnect Calling BeginInvoke from {0}.", threadId)

    // Initiate the asychronous call. Include an AsyncCallbac
    // delegate representing the callback method, and the dat
    // needed to call EndInvoke
    ar = dlgt.BeginInvok e( out threadId, new AsyncCallback(A utoConnectCallb ack), dlgt )

    bRetVal = ar.IsCompleted

    return bRetVal



    public int AutoConnect(out int threadId)

    int ret = 0
    Console.WriteLi ne("Begin Call to: AutoConnect.")

    threadId = AppDomain.GetCu rrentThreadId()

    // ActiveX call that blocks
    m_driver.AutoCo nnect()

    Console.WriteLi ne("End Call to: AutoConnect.")
    return ret


  • Willy Denoyette [MVP]

    #2
    Re: async call to ActiveX is still blocking UI

    Using asynch callbacks won't help, probably your activeX object lives on the
    UI thread, and the calls are made from the threadpool thread, which is bad
    for a number of reasons
    - Your set-up requires apartment marshaling - threadpool threads are MTA
    while UI thread is STA, and can be a source of numerous threading problems
    and perf degradation.
    - You will still block the UI while running activeX methods.

    What you should do instead of using asynch delegates is create a separate
    thread initialized for STA, create an instance of your activeX object and
    call his methods on that same thread. Note that if you need to access the UI
    thread from this thread, you need to call Control.Invoke or
    Control.BeginIn voke.

    Willy.


    "Marwan" <anonymous@disc ussions.microso ft.com> wrote in message
    news:C18FD887-9DFC-4EAC-91DC-5972EC4ED851@mi crosoft.com...[color=blue]
    > Hello,
    >
    > I am using asynchronous delegates to make a call to a COM ActiveX object,
    > but even though the call occurs on a separate thread, my UI is still
    > blocking.
    >
    > If i put the thread to sleep in my delegate call, the application is well
    > behaved (no UI freeze), but the call to the com object causes the UI to
    > lock up.
    >
    > Do I have to manage calls to an ActiveX object differently than using the
    > BeginInvoke and a callback?
    >
    > A sample of the code I am using:
    > /// <summary>
    > // Connect to the device
    > /// </summary>
    > /// <returns></returns>
    > public bool BeginConnect()
    > {
    > bool bRetVal = false;
    > IAsyncResult ar;
    > int threadId;
    >
    > // Create the delegate.
    > AsyncDelegate dlgt = new AsyncDelegate(m _ad.AutoConnect );
    >
    > threadId = AppDomain.GetCu rrentThreadId() ;
    > Console.WriteLi ne("In BeginConnect Calling BeginInvoke from {0}.",
    > threadId);
    >
    > // Initiate the asychronous call. Include an AsyncCallback
    > // delegate representing the callback method, and the data
    > // needed to call EndInvoke.
    > ar = dlgt.BeginInvok e( out threadId, new
    > AsyncCallback(A utoConnectCallb ack), dlgt );
    >
    > bRetVal = ar.IsCompleted;
    >
    > return bRetVal;
    > }
    >
    >
    >
    > public int AutoConnect(out int threadId)
    > {
    > int ret = 0;
    > Console.WriteLi ne("Begin Call to: AutoConnect.");
    >
    > threadId = AppDomain.GetCu rrentThreadId() ;
    >
    > // ActiveX call that blocks!
    > m_driver.AutoCo nnect();
    >
    > Console.WriteLi ne("End Call to: AutoConnect.");
    > return ret;
    > }
    >[/color]


    Comment

    Working...