views:

25

answers:

1

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 performance but don't know how to check the state of the COM app from the calling thread.

Hope this makes sense and TIA.

+2  A: 

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.

Hans Passant
Fair enough. I had a feeling it would be stuck with it. The app is running as expected, I am just trying to make it go faster. Unfortunately, I have no choice but to use this COM object in this style. Thanks.
alex.davis.dev
This is very, very unwise. You have no idea what state your program is in when the component blows up. The typical failure mode is a corrupted heap, can't catch and fix that. Well, good luck
Hans Passant