Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using WPF (which requires STAThread) with an API that can't work with STAThread

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. :|

like image 303
Rune Jacobsen Avatar asked Dec 23 '22 10:12

Rune Jacobsen


2 Answers

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();
like image 163
Marc Gravell Avatar answered May 19 '23 20:05

Marc Gravell


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() );
like image 25
pjbelf Avatar answered May 19 '23 19:05

pjbelf