Using system; using system. threading; using system. windows. forms; namespace webbrowsertest {public partial class mainform: FORM {public mainform () {initializecomponent ();} public static childform; private void mainform_load (Object sender, eventargs e) {This. TEXT = string. format ("thread ID: {0}", thread. currentthread. managedthreadid);} private void button#click (Object sender, eventargs e ){ Thread thread = new thread (New threadstart (this. createform); thread. setapartmentstate (apartmentstate. sta); thread. isbackground = true; thread. start ();} private void createform () {mainform. childform = new childform (); mainform. childform. webbrowser. navigate ("http://tieba.baidu.com/"); // here is not the UI thread, why can navigation? Mainform. childform. showdialog ();} private void button2_click (Object sender, eventargs e) {If (mainform. childform! = NULL & mainform. childform. ishandlecreated) {// if you do not call the invoke method in the childform window to modify the title, an exception is reported! // The Inter-thread operation is invalid: It is accessed by a thread that does not create the control "childform. Mainform. childform. invoke (New methodinvoker (delegate {mainform. childform. TEXT = string. format ("thread ID: {0}", thread. currentthread. managedthreadid) ;}); // Why can I navigate the page normally? // Is the webbrowser Control always running in the UI thread? // Isn't the UI thread even if it is created? Mainform. childform. webbrowser. navigate ("http://www.baidu.com /");}}}}
But what's amazing is that you can operate webbrowser to navigate the webpage without calling the invoke method or reporting exceptions. Why?
In this environment, what thread does webbrowser run in? Main thread? Or a new sub-thread?
After the code is simplified, the result is the same.
Using system; using system. threading; using system. windows. forms; namespace webbrowsertest {public partial class mainform: FORM {public mainform () {initializecomponent ();} private form childform; private webbrowser; private void mainform_load (Object sender, eventargs E) {This. TEXT = string. format ("thread ID: {0}", thread. currentthread. managedthreadid);} private void button#click (Object Sen Der, eventargs e) {thread = new thread (New threadstart (this. createform); thread. setapartmentstate (apartmentstate. sta); thread. isbackground = true; thread. start ();} private void createform () {This. childform = new form (); this. webbrowser = new webbrowser (); this. webbrowser. dock = dockstyle. fill; this. childform. controls. add (this. webbrowser); this. webbrowser. navigate ("http://tieba.baidu.com /"); // This Is Not a ui thread. Why can we navigate? This. childform. showdialog ();} private void button2_click (Object sender, eventargs e) {If (this. childform! = NULL & this. childform. ishandlecreated) {// if you do not call the invoke method in the childform window to modify the title, an exception is reported! // The Inter-thread operation is invalid: It is accessed by a thread that does not create the control "childform. This. childform. invoke (New methodinvoker (delegate {This. childform. TEXT = string. format ("thread ID: {0}", thread. currentthread. managedthreadid); // The modification is normal. }); This. childform. Text = "modify Title"; // an exception is reported when the title is modified! // Why can I navigate the page normally? // Is the webbrowser Control always running in the UI thread? // Isn't the UI thread even if it is created? This. webbrowser. navigate ("http://www.baidu.com /");}}}}
To answer this question,
Purpose:
Now the multi-tag browser, each tag is executed by a thread.
The crash of one of the tag pages does not affect other tag pages.
The new thread creates a window with the webbrowser control,
Multi-thread acceleration,
If a webpage crashes, the entire program will not be closed.
Source code download: http://files.cnblogs.com/kofip/WebBrowserTest.rar
Simplified Source: http://files.cnblogs.com/kofip/WebBrowserTest2.rar
Finally, I figured out that in the. NET 1.0 era, non-UI threads can directly operate on UI controls.
You cannot directly operate the UI control at the beginning of. NET 2.0,
However, webbrowser is a system COM component and is not added by the. NET team. Check the operation thread mechanism.
However, in essence, such problems should also exist, but they have not been detected internally.
Using system; using system. threading; using system. windows. forms; namespace webbrowsertest {public partial class mainform: FORM {public mainform () {initializecomponent ();} private form childform; private webbrowser; private void mainform_load (Object sender, eventargs E) {This. TEXT = string. format ("thread ID: {0}", thread. currentthread. managedthreadid);} private void button#click (Object Sen Der, eventargs e) {thread = new thread (New threadstart (this. createform); thread. setapartmentstate (apartmentstate. sta); thread. isbackground = true; thread. start ();} private void createform () {This. childform = new form (); this. webbrowser = new webbrowser (); this. webbrowser. dock = dockstyle. fill; this. webbrowser. documentcompleted + = new webbrowserdocumentcompletedeventhandler (this. webbrows Er_documentcompleted); this. childform. controls. add (this. webbrowser); // This. webbrowser. navigate ("http://tieba.baidu.com/"); // here is not the UI thread, why can navigation? This. childform. showdialog ();} private void button2_click (Object sender, eventargs e) {If (this. childform! = NULL & this. childform. ishandlecreated) {// if you do not call the invoke method in the childform window to modify the title, an exception is reported! // The Inter-thread operation is invalid: It is accessed by a thread that does not create the control "childform. Int threadid = thread. currentthread. managedthreadid; this. childform. invoke (New methodinvoker (delegate {This. childform. TEXT = string. format ("main thread ID: {0} sub-thread ID: {1}", threadid, thread. currentthread. managedthreadid); // The modification is normal. }); // This. childform. Text = "modify Title"; // an exception is reported when the title is modified! // Why can I navigate the page normally? // Is the webbrowser Control always running in the UI thread? // Isn't the UI thread even if it is created? This. webbrowser. navigate ("http://www.baidu.com/") ;}} private void webbrowser_documentcompleted (Object sender, webbrowserdocumentcompletedeventargs e) {If (this. webbrowser. readystate = webbrowserreadystate. complete) {MessageBox. show (string. format ("webbrowser thread ID: {0}", thread. currentthread. managedthreadid ));}}}}