I have a c# 4.0 library that is instantiated by a C++ MFC client and I am trying to use a the TPL to provide an asynchronous task facility that releases the client's thread and ultimately raises a completed event on the clients 'Main Thread'.
Because I have no UI there is no Synch Context so the old FromCurrentSynchronisationConext problem arises. I figured I could install a context if I can be sure the client is a COM STA client so I have read many posts about how to tell if you are on the 'Main Thread' but the suggested tests (i.e. How to tell if a thread is the main thread in C#) fails because surprisingly we appear to be on a background thread. The Thread ID is 1, it is STA, it is not a ThreadPool thread and it is alive but why is it thinking it is a background thread??
Assuming I ignore that part of the test and continue to install a new WindowsFormsSynchronizationContext, is this safe? Essentially I'm saying 'OK this is our C++ app's thread so if I stick this context in I can pretend it's a .Net UI thread and marshal calls back to it'
Is there a safe way of doing this?
One last thing...why does the C++ app appear to receive my background thread raised events on the main thread anyway!! Is it even worth the effort of trying to marshal or just let it 'magically' happen?
Losing my mind so any help would be appreciated!!