Question

I have this simple code:

public void Run()
{
   var invokerThread = new Thread(new ThreadStart(RunOnBackground));
   invokerThread.Start();
}

private void RunOnBackground()
{
   Trace.WriteLine("hi");
   ...
}

Unfortunately when running this code (from third party process) the thread doesn't really run. Either in process explorer and in VS debugger I see that the thread is created and its state is "Running".

The main thread's apartment is STA and I've tried both STA and MTA on internal thread.

When I add to the Run() method at the end invokerThread.Join(); then the thread does run. But then again it doesn't really help.

What am I missing?

Edit: Here is some more information regarding the code hosting -

Run() method is called via COM interop from a process which is also managed executable assembly (the reason COM interop is used is because all other components in the system are native).

The method RunOnBackground() includes some more code after the tracing and generally its execution lasts between 10 - 20 seconds, including starting another process and waiting for its termination. Also I have some other areas in the code where I write some debug information to the Trace. While debugging the code, Run() runs as usual and after invokerThread.Start(); invokerThread's state is "Running" (though breakpoints inside the RunOnBackground() method don't stop).

When I add invokerThread.Join() at the end of the Run() method the debugger goes to RunOnBackground() after the Join().

Was it helpful?

Solution

There's some crucial information missing about what RunOnBackground() really does. This is otherwise a good match for what happens when you use apartment threaded COM objects on a worker thread. COM automatically marshals any method call on such an object from the worker thread to the STA thread on which it was created.

That can only work well when the STA thread observes STA threading requirements. It must pump a message loop and cannot block. Breaking those rules makes deadlock very likely, the worker thread call cannot complete until the STA thread dispatches the marshaled call. A sure sign that this is what is going on is seeing Thread.Join() solve the problem. It pumps a message loop internally in the CLR when it is called on an STA thread.

To diagnose this, you'll need Debug + Windows + Threads to see what that worker thread is blocking on. If my guess is right, it will be buried deep inside of the COM plumbing code, waiting for the marshaled call to complete. You can only see this by enabling unmanaged code debugging and setting up the Microsoft Symbol Server so you get debugging symbols for the plumbing code and get a reliable stack trace.

Fixing this is going to be difficult. You cannot magically flip a switch and make code run on a thread when it has explicitly stated that it doesn't support multi-threading. It is imperative that you create the instance of the COM object on the same thread that calls its methods. And that thread has to be an STA thread. Check this sample code for the approach. If you don't control the creation of the COM object then you're stuck.

OTHER TIPS

I may say something stupid, but here is what I saw in MSDN Threads.

Look at the examples section at the end.

The output of the example is interesting, you can see there that the Thread created and started only starts executing when the main thread does a Sleep(0) or a Thread.Join().

It seems to be what exactly happens to you, doesn't it ?

Maybe try with a Sleep(0) on your main thread to really launch your working Thread.

Another workaround would be to use the BackGroundWorker.

As its name says it, it works on the Background and is really easy to use. It may be of much use to you.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top