kusanagi kusanagi - 2 months ago 23
C# Question

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.

Answer

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 (Get/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();

//daniel

Comments