How to detect if a COM application is currently running?
I have a multi-threaded application using .NET 4.0. Sometimes this COM application referenced freaks out and just stop.
I want to know how to check for to see if this process is currently running within the questioned thread.
Example:
Exert from the main application:ThreadedApp app = new ThreadedApp();
System.Threading.Thread thread = new System.Threading.Thread(x => app.Start());
thread.Start();
Business Object exert:
class ThreadApp
{
public void Start()
{
COMobject app = new COMobject(); // <- COM interop that starts up the COM app
foreach (var item in items)
{
new ThreadProcess().Process(ref app, item);
}
}
}
class ThreadProcess
{
public void Process(ref COMobject app, SomeItem item)
{
if (app == IVE_DIED_AND_I_DONT_KNOW_WHY) // How do I check this?
{
app = new COMobject();
}
// Process stuff
}
}
The main app can spin off N number of threads and therefore can spin off N number of the COMobject application. If you watch the Task Manager, you would see the COMobject.exe listed for each thread running.
I am trying to get away from try/catch blocks to increase perform开发者_开发百科ance but don't know how to check the state of the COM app from the calling thread.
Hope this makes sense and TIA.
Try/catch is all you've got. The try is for free, only the catch is expensive. Should not matter.
In general, be very careful mixing COM objects and threads. The vast majority of them do not support free threading. Doing it anyway buys you exactly the kind of problem you are having.
精彩评论