Search code examples
c#multithreadingwatin

WatIn multithreading


How can I work with WatIn using async delegates? I tried, but it returned this error:

The CurrentThread needs to have it's ApartmentState set to ApartmentState.STA to be able to automate Internet Explorer.


Solution

  • I assume you use BeginInvoke() on your delegates. They use the thread pool to do threaded work and the threads in the thread pool are all MTA's. You will have to do it the old fashion way by creating your own Thread. The thread class offers methods (GetApartmentState and SetApartmentState) to change the apartment model.

    I guess you might need your own message pump as well in your thread.

    Something like this might get you started:

    var th = new Thread(() => { /* do work */ });
    th.SetApartmentState(ApartmentState.STA);
    th.Start();