我如何使用異步委託來處理Watin?我試過,但它返回此錯誤:watin多線程
The CurrentThread needs to have it's ApartmentState set to ApartmentState.STA to be able to automate Internet Explorer.
我如何使用異步委託來處理Watin?我試過,但它返回此錯誤:watin多線程
The CurrentThread needs to have it's ApartmentState set to ApartmentState.STA to be able to automate Internet Explorer.
我假設你對你的代表使用的BeginInvoke()。他們使用線程池進行線程化工作,線程池中的線程都是MTA的。你將不得不通過創建自己的主題來以舊時尚的方式來完成它。線程類提供方法(Get/SetApartmentState)來更改公寓模型。
我想你可能還需要你自己的消息泵。
像這樣的東西可能讓你開始:
var th = new Thread(() => { /* do work */ });
th.SetApartmentState(ApartmentState.STA);
th.Start();
//丹尼爾
,因爲如何在IE COM互操作的作品,它本身需要在單線程單元要運行的性質。以下是從[watin.org]頁上覆制對象1
Why is setting the ApartmentState needed in the first place?
Copied from MSDN:
「Because COM classes use apartments, the common language runtime needs to create and initialize an apartment when calling a COM object in a COM interop situation. A managed thread can create and enter a single-threaded apartment (STA) that allows only one thread, or a multithreaded apartment (MTA) that contains one or more threads.」.
Since Internet Explorer is not Thread safe we need to use STA.
Implications
Using WatiN works fine in a console or GUI application when you apply the [STAThread] attribute to the main method (the sole entry point of the application). This way the main Thread runs as STA and all goes well. When using a runner like MBUnit, NUnit or any other runner that starts the main Thread, your code/tests depend upon the ApartmentState the runner started with.
Behaviour of WatiN when Thread is not an STA Thread.
WatiN will throw a ThreadStateException when you create an instance of the WatiN.Core.IE class, to help you remind to set the ApartmentState to STA.
因爲我知道線程不允許傳遞參數 – kusanagi 2011-03-31 15:30:41
如果你在談論線程的構造函數,我認爲你錯了。如果你在談論別的事情,你能否澄清一下?我沒有看到問題。 – Daniel 2011-04-01 07:02:46