views:

707

answers:

3

I am writing a WPF application that has an optional dependency on an API that has a simple requirement; It MUST be initialized/used on a thread that does NOT have the STAThread attribute. Of course, WPF requires STA so that makes everything easy.

In this case, WPF is required no matter what. This third party API is required only when the user chooses to enable this functionality in the application. This means that the WPF application is already running once it is time to invoke the other API.

If you do not decorate your main method with [STAThread], is it automatically an MTA thread? In this case, does that mean I can create a new MTA thread and use the other API on it?

If that would work, then I guess that any events from this API could talk to the WPF app using the Dispatcher (for raising events that need to show in the UI etc.). However, is there a simple way that my WPF app can "invoke" functionality on the MTA thread to make API calls?

In MTA I guess that every thread should be able to play with state, but I guess my STA thread (WPF app) can't just "reach into" the MTA thread and perform API calls?

There is so much possibility for confusion here that I would love some input on how to design something like this!

Thanks!

[Edit July 8th]

Okey, I had some concepts confused in the above. The threading model is of course set for a PROCESS, not for each thread, and this third party API can't work with STA process.

Currently, the only way I see out of this mess is to write a service that communicates with this API, then communicate with this service using named pipes. This is not trivial at all, an ugly ugly workaround, but the third party API is not under my control. Such is life. :|

+2  A: 

You will need to spin up a new thread to call your code that requires an MTA thread. You could try something like the following to make it easy to invoke on an mta thread. This will block the main thread while the MTA thread is executing. How efficient this is will depend on your API usage as you are spinning up a new thread everytime you call it.

public delegate void MtaMethod();

public class MtaHelper
{

    public static void RunMta(MtaMethod method)
    {

        ManualResetEvent evnt = new ManualResetEvent(false);

        Thread thread = new Thread(delegate()
        {
            method();
            evnt.Set();
        });

        thread.SetApartmentState(ApartmentState.MTA);
        thread.Start();
        evnt.WaitOne();
    }

}

and then you should be able to call your api as

MtaHelper.RuntMta( () => OtherAPIMethod() );
pjbelf
Hi and thanks for your suggestion. The only problem I have with this is the blocking, as the other API is of the type that can take some time to complete. I will try this as well tho!
Rune Jacobsen
+2  A: 

Is the external API a GUI API? Or just functions etc? If the latter, then you could spawn a second thread (perhaps using a producer/consumer queue) that is MTA:

    Thread thread = new Thread(DoSomeStuff);
    thread.SetApartmentState(ApartmentState.MTA);
    thread.Name = "3rd aprty API spooler";
    thread.Start();
Marc Gravell
Thanks Marc - I will try this. The external API is a phone system, where functionality/events will take some time (not seconds, but noticeable) to execute, so this looks like a good plan.
Rune Jacobsen
A: 

I exploit the DispatcherObject in WPF.

I make a subclass of DispatcherObject, then create an instance using an MTA thread. This way, I can use mySubclass.Invoke(...) or mySubclass.BeginInvoke(...) and the executing code will happen on my MTA thread.

You can see an example here:

http://wpfmediakit.codeplex.com/SourceControl/changeset/view/24019#283908

Take special note of the CreateDvdPlayer static method, and also look at the base class in BaseClasses.cs

Jeremiah Morrill