async call to ActiveX is still blocking UI

G

Guest

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.GetCurrentThreadId()
Console.WriteLine("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.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
 
W

Willy Denoyette [MVP]

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top