async call to ActiveX is still blocking UI
Messages   Related Types
This message was discovered on microsoft.public.dotnet.general.
Responses highlighted in red are from those people who are likely to be able to contribute good, authoratitive information to this discussion. They include Microsoft employees, MVP's and others who IMHO contribute well to these kinds of discussions.
Post a new message to this list...

Marwan
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.GetCurrentThreadId();
    Console.WriteLine("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.BeginInvoke( out threadId, new AsyncCallback(AutoConnectCallback), dlgt );

    bRetVal = ar.IsCompleted;

    return bRetVal;
}

public int AutoConnect(out int threadId)
{
    int ret = 0;
    Console.WriteLine("Begin Call to: AutoConnect.");

    threadId = AppDomain.GetCurrentThreadId();

    // ActiveX call that blocks!
    m_driver.AutoConnect();

    Console.WriteLine("End Call to: AutoConnect.");
    return ret;
}

Reply to this message...
 
    
Willy Denoyette [MVP] (VIP)
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.BeginInvoke.

Willy.

"Marwan" <Click here to reveal e-mail address> wrote in message
news:Click here to reveal e-mail address...
[Original message clipped]

Reply to this message...
 
 
System.AppDomain
System.AsyncCallback
System.Console
System.IAsyncResult
System.Web.UI.Control




Ad
MBR BootFX
Best-of-breed application framework for .NET projects, developed by Matthew Baxter-Reynolds and MBR IT
 
 Copyright © Matthew Baxter-Reynolds 2001-2008. '.NET 247 Software Development Services' is a trading style of MBR IT Solutions Ltd.
Contact Us - Terms of Use - Privacy Policy - www.dotnet247.com