0

I need to embed a out-of-prcess COM execution in my C# application. The COM execution is written in C++/ATL. Sometimes my COM create process will not start, so I manage to use a timeout to interrupt it.

My COM object initializing process:

using MyCOM;
//...
private MyCOM.Mylib _com;  // COM object
private void MyInitJob()
{
    _com = new MyCOM.Mylib();            
}
private bool Initial()
{
    System.Threading.Thread mythread = new System.Threading.Thread(MyInitJob);
    mythread.Start();
    if (!mythread.Join(5000))
    {
        mythread.Abort();
        return false;
    }
    else
    {
        return true;
    }
}

The above code seems could not stop MyInitJob() thread while timeout is reached, and Initial() will hang forever when new a COM object is stuck.

Could I know why new takes forever, and could I interrupt when it takes too long?

Dia
  • 851
  • 1
  • 15
  • 35
  • What is the threading model of the COM object? Maybe you need a `[STAThread]`? –  Apr 17 '18 at 09:53
  • This code is just a band-aid, it cannot stop the bleeding. Thread.Abort() can only actually abort a thread when it is busy executing safe code. C++ code is not safe, the Abort() call will just hang. Nor is there anything reasonable you can do next but terminate the program. You need to debug the C++ code. Enable unmanaged debugging or start VS again and attach to the unmanaged server process. – Hans Passant Apr 17 '18 at 10:29
  • The COM object is a third-party execution working "as it", seems hard to debug this execution. – Dia Apr 18 '18 at 02:17

0 Answers0