This is in a WinForm program, click a button on the interface to generate another thread, generate WebBrowser control, and increase event monitoring
private void button1_Click(object sender, EventArgs e)
{
Thread t = new Thread(new ThreadStart(m)); t.Start();
}
private void m()
{
WebBrowser w = new WebBrowser();
w.DocumentCompleted +=new WebBrowserDocumentCompletedEventHandler(w_DocumentCompleted);
w.Navigate("www.baidu.com");
}
private void w_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
MessageBox.Show("OK");
}
When the program is running, you can find that there is no ok window pop-up. If you set the breakpoint, you will find that there is no w at all_ Run in the documentcompleted method. The reason is that the thread of the browser ends after executing the m method, and there is no time to execute the event listening method. At this point, there is a way to use the message box to make the thread temporarily not disappear
private void button1_Click(object sender, EventArgs e)
{
Thread t = new Thread(new ThreadStart(m));
t.ApartmentState = ApartmentState.STA;
t.Start();
}
private void m()
{
WebBrowser w = new WebBrowser();
w.DocumentCompleted +=new WebBrowserDocumentCompletedEventHandler(w_DocumentCompleted);
w.Navigate("www.baidu.com");
MessageBox.Show("AAA");
}
private void w_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
MessageBox.Show("OK");
}
Now, if the thread does not disappear after AAA, then the event will be monitored and OK will appear
Read More:
- Spring boot does not take effect when using @ size to verify a single set parameter of the control layer
- Node start – create a server listening port.
- java.lang.IllegalStateException: Could not execute method for android:onClick
- Idea start error: java.lang.IllegalStateException : failed to create a child event loop
- [Solved] VUE3.0 Warning: Added non-passive event listener to a scroll-blocking ‘mousewheel‘ event如何解决
- [reprint and save] MySQL does not set the primary key and uses the self growing ID method
- The problem that headers [‘content-type ‘] does not work is set in the Axios get method request interface
- TypeError: Failed to execute ‘fetch‘ on ‘Window‘: Request with GET/HEAD method cannot have body.
- Why do we not need to write implementation classes in mybatis to get the objects we need
- Module build failed Error Plugin/Preset files are not allowed to export objects, only functions (How to Fix)
- VUEJS Failed to execute ‘removeChild’ on ‘Node’: The node to be removed is not a child of
- Job for docker.service failed because the control process exited with error code. See systemctl sta
- Set code indent to space indent in eclipse
- Error code: events.js:183 throw er; // Unhandled’error’ event—solution
- Job for network.service Failed because the control process exited with error code. See “SystemC
- error PRJ0019: A tool returned an error code from “Performing Post-Build Event…”
- react Error: Objects are not valid as a React child (found: object with keys {username, password})
- SteamVR error code 108/203/208/301/306/308/400/405 troubleshooting method
- “Method does not override method from its superclass”
- Apache2 cannot be started and an error is reported for apache2.service failed because the control process exited with error code.