我有一個WebBrowser實例的線程,並附加了一個DocumentCompleted事件。但從我觀察到的情況來看,我的事件並沒有發生,因爲線程在它發生之前就結束了。當我將MessageBox.Show放在線程的末尾時,它會給事件提出時間。但是如何讓線程在沒有MessageBox的情況下等待呢?如何保持線程活着,直到WebBrowser事件觸發
WebBrowser browser;
Thread t = new Thread(() =>
{
string url = string.Format("webpage.com");
browser = new WebBrowser();
browser.ScriptErrorsSuppressed = true;
browser.Navigate(url);
browser.DocumentCompleted += browser_DocumentCompleted;
browser.DocumentCompleted += (o, a) =>
{
//MessageBox.Show("In DocumentCompleted.");
List<Status> statusy = new List<Status>();
IHTMLDocument2 currentDoc = (IHTMLDocument2)browser.Document.DomDocument;
//parsing the html doc
string Statuses = "";
foreach (Status status in statusy)
{
Statuses += String.Format("{0} {1} - {2} --> {3}{4}", status.Date, status.Time, status.Centre, status.Message, Environment.NewLine);
}
MessageBox.Show(Statuses);
};
MessageBox.Show("In thread!!!!");
});
t.SetApartmentState(ApartmentState.STA);
t.Start();
你在使用什麼.net版本?它是一個控制檯應用程序? – Sievajet
4.0,這個代碼來自控制檯應用程序,但是當它工作時它會在類文件中(在Windows窗體應用程序中,但沒有訪問實際的表單)。 – Dess
您使用哪個MessageBox來等待? –