Today, in the garden, we can see that the school School's ass uses C # webbrowser and application. doevents () to collect dynamic web pages.
In fact, I used a similar method to capture the web pages to be logged on, and dynamic page adding pages.
What I want to talk about today is how to implement multithreading and use webbortracing to collect pages.
Among them, I used a weifenluo. winformsui. Docking. dll, which is an open source component.
: Https://github.com/dockpanelsuite/dockpanelsuite/downloads
Some predecessors in the garden have made detailed usage of this component. I will not explain it more here.
Http://www.cnblogs.com/wuhuacong/archive/2009/07/09/1520082.html
Add a weifenluo control to the mainform, and set the ismdicontainer attribute of the mainform to true.
Add the addwebform method to dynamically Add sub-forms.
public void addWebForm(string s){ if (this.InvokeRequired) { this.BeginInvoke(new OneStringParmenters(addWebForm), s); } else { ChildForm f2 = new ChildForm(); f2.Text = s; webForm.Add(s, f2); f2.Show(dockPanel1); }}
Add a webborwser control to the childform.
And add the method to open the page
Public Delegate void onestringparmenters (string Str); // one string parameter delegates public void navigate (string URL) {If (this. invokerequired) {This. begininvoke (New onestringparmenters (navigate), URL);} else {webbrowser1.navigate (URL );}}
And how to read html
Private string strhtmlleng = ""; private delegate void noparameters (); // No parameter delegate; Public String strhtmlleng {get {If (this. invokerequired) {iasyncresult IAR = This. begininvoke (New noparameters (gethtmlleng); While (! IAR. iscompleted) {system. threading. thread. sleep (0) ;}} else {gethtmlleng ();} return strhtmlleng ;}} private void gethtmlleng () {strhtmlleng = webbrowser1.documenttext ;}
Add a multithreading method to simulate multithreading to open a page and read html
Public static void openwebpage (Object strurl) {datetime dt = datetime. now; string u = strurl. tostring (); iasyncresult IAR = program. form. begininvoke (New onestringparmenters (program. form. addwebform), U); While (! IAR. iscompleted) {system. threading. thread. sleep (0);} int n = program. form. webform. count; program. form. webform [u]. navigate (U); system. threading. thread. sleep (timespan. fromminutes (1); string strhtml = program. form. webform [u]. strhtmlleng; // output HTML to the local edisk. When collecting information, you can directly operate the HTML system. io. streamwriter Sw = new streamwriter (string. format ("E:/1_02.16.txt", u); Sw. write (string. format ("Start Time: {0} \ r \ n End Time: {1} \ r \ n open information: {2}", DT. tostring ("G"), datetime. now, strhtml); Sw. dispose ();}
Now we can use multiple threads to execute the openwebpage method to simulate multi-threaded webborwser operations.
Here is the small program I tested and written.
Http://files.cnblogs.com/jiangming/TestThreadWebBrowser.zip