COMException and Multiple Threads

  • Thread starter Michael E. Pouliot
  • Start date
M

Michael E. Pouliot

My VB.NET application spins up a secondary GUI thread. This thread hosts a
Custom User Control (which owns the window handle) and some COM objects
which interact with a Third Party Application. When a method call to one of
the COM objects results in an error, the .NET Interop throws a
System.Runtime.InteropServices.COMException, as one would expect. However,
while generating this exception, both the main and secondary GUI threads are
halted. I can find no explanation for why the main thread should halt
during this time. The entire purpose of spinning up a secondary GUI thread
was to allow the primary GUI thread to stay active while COM related
processing was occurring. Does anyone know what is happening under the
covers of the COM Interop that would cause this? This pause (of about three
seconds) only occurs when the first COMException is thrown. Subsequent
exceptions do not cause the entire application to halt.

Thanks,
MP
 
W

Willy Denoyette [MVP]

Guess you are running in a debugger, which would explain this behavior. Try
to run in release mode.

Willy.
 
W

Willy Denoyette [MVP]

To add to my previous post, if your COM object is STA threaded, you have to
make sure you secondary thread is initialized accordingly. Failing to
initialize a second thread to enter a STA will will instantiate the object
in the Main thread STA.

Willy.
 
M

Michael E. Pouliot

Willy Denoyette said:
Guess you are running in a debugger, which would explain this behavior. Try
to run in release mode.

Willy.

Willy Denoyette said:
To add to my previous post, if your COM object is STA threaded, you have to
make sure you secondary thread is initialized accordingly. Failing to
initialize a second thread to enter a STA will will instantiate the object
in the Main thread STA.

Willy.
Thanks for replying. When I was first testing this, I noticed that the
issue was occurring in both debug and release. I had since changed my code
to explicitly initialize the secondary thread as an STA thread, but had not
checked if this affected the release version. The release version now
appears to act as expected.

If anyone can direct me to documentation detailing why the project runs
differently in debug than in release, I'd appreciate it. I'd like to gain a
better understading of how the .NET Interop handles the creation of a COM
object on a particular thread. It seems odd that in the debugger would
handle COM object creation differently than the release code, but I imagine
there are significant challenges to doing it otherwise.
 

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