/
BrowserWrapper.cs
178 lines (163 loc) · 5.14 KB
/
BrowserWrapper.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.Windows.Forms;
public partial class browserwrapper : System.Windows.Forms.WebBrowser
{
/// <summary>
/// Designer variable used to keep track of non-visual components.
/// </summary>
private System.ComponentModel.IContainer components;
/// <summary>
/// Disposes resources used by the control.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing) {
if (components != null) {
components.Dispose();
}
}
base.Dispose(disposing);
}
/// <summary>
/// This method is required for Windows Forms designer support.
/// Do not change the method contents inside the source code editor. The Forms designer might
/// not be able to load this method if it was changed manually.
/// </summary>
private void InitializeComponent()
{
//
//browserwrapper
//
//Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
this.Name = "browserwrapper";
}
/// <summary>
/// This event is raised when the current document is entirely complete,
/// and no more navigation is expected to take place for it to load.
/// </summary>
/// <author>hmcclungiii</author>
/// <date>2/21/2014</date>
public event AbsolutelyCompleteEventHandler AbsolutelyComplete;
public delegate void AbsolutelyCompleteEventHandler(object sender, EventArgs e);
private int _onNavigatingCount = 0;
private int _onNavigatedCount = 0;
private int _onDocumentCompleteCount = 0;
/// <summary>
/// This method is used to clear the counters. Should not be used
/// externally, but I left it open for testing, and just in case
/// scenarios
/// </summary>
/// <author>hmcclungiii</author>
/// <date>2/21/2014</date>
public void ClearCounters()
{
_onNavigatingCount = 0;
_onNavigatedCount = 0;
_onDocumentCompleteCount = 0;
}
/// <summary>
/// This property returns true when all the counters have become equal
/// signifying that the navigation has completely completed
/// </summary>
/// <author>hmcclungiii</author>
/// <date>2/21/2014</date>
public bool Busy {
get {
//sometimes the first navigating event isn't fired so we just have to make sure the navigating count is
//more than the navigated, navigated should never be more than navigating
bool bBusy = !(_onNavigatingCount <= _onNavigatedCount);
//if our navigating counts check out, we should always have a documentcompleted count
//for each navigated event that is fired
if (!bBusy)
{
bBusy = (_onNavigatedCount > _onDocumentCompleteCount);
}
else
{
bBusy = !(_onNavigatedCount == _onDocumentCompleteCount);
if (!bBusy) bBusy=!(_onNavigatedCount>0);
}
return bBusy;
}
}
/// <summary>
/// This method is used to wait until the page has completely loaded. Use
/// after calling a submit, or click, or similar method to not execute further
/// code in the calling class until it has completed. Also helps to reduce
/// processor load
/// </summary>
/// <author>hmcclungiii</author>
/// <date>2/21/2014</date>
public void WaitUntilComplete()
{
//first we wait to make sure it starts
while (!Busy) {
Application.DoEvents();
//we should sleep for a moment to let the processor have a timeslice
//for something else - in other words, don't hog the resources
System.Threading.Thread.Sleep(1);
}
//now we wait until it is done
while (Busy) {
Application.DoEvents();
//we should sleep for a moment to let the processor have a timeslice
//for something else - in other words, don't hog the resources
System.Threading.Thread.Sleep(1);
}
}
public browserwrapper()
{
this.InitializeComponent();
}
//we have to catch the following three event callers to keep a count
//of them so that we will be able to determine when the navigation
//process actually completes
protected override void OnNavigating(WebBrowserNavigatingEventArgs e)
{
_onNavigatingCount += 1;
base.OnNavigating(e);
if (!Busy)
OnAbsolutelyComplete();
}
protected override void OnNavigated(WebBrowserNavigatedEventArgs e)
{
_onNavigatedCount += 1;
base.OnNavigated(e);
if (!Busy)
OnAbsolutelyComplete();
}
protected override void OnDocumentCompleted(WebBrowserDocumentCompletedEventArgs e)
{
_onDocumentCompleteCount += 1;
base.OnDocumentCompleted(e);
if (!Busy)
OnAbsolutelyComplete();
}
/// <summary>
/// This method should be used in place of the Navigate method to navigate to
/// a specific URL. The navigate method was not overridden because it might
/// be required in future modifications to have access to both methods. When
/// calling this NavigateAndWait method, control will not be returned to the
/// calling class until the document has completely loaded
/// </summary>
/// <author>hmcclungiii</author>
/// <date>2/21/2014</date>
public void NavigateAndWait(string URL)
{
ClearCounters();
Navigate(URL);
WaitUntilComplete();
}
protected void OnAbsolutelyComplete()
{
ClearCounters();
if (AbsolutelyComplete != null) {
AbsolutelyComplete(this, new EventArgs());
}
}
}