forked from IgorGorbunov/RABot
/
NetClass.cs
192 lines (176 loc) · 5.66 KB
/
NetClass.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
179
180
181
182
183
184
185
186
187
188
189
190
191
using System.ComponentModel;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Net.NetworkInformation;
using System.Windows.Forms;
using System.Windows.Media.Imaging;
using RABot.Models;
public static class NetClass
{
private static BackgroundWorker _netConnectionWorker;
private static System.Windows.Controls.Image _img1, _img2;
private const string Site1 = "mail.ru";
private const string Site2 = "smart-lab.ru";
public static void StartPing(System.Windows.Controls.Image img1, System.Windows.Controls.Image img2)
{
_img1 = img1;
_img2 = img2;
_netConnectionWorker = new BackgroundWorker
{
WorkerReportsProgress = true,
WorkerSupportsCancellation = true
};
_netConnectionWorker.DoWork += NetConnectionWorker_DoWork;
_netConnectionWorker.RunWorkerCompleted += NetConnectionWorker_RunWorkerCompleted;
_netConnectionWorker.ProgressChanged += NetConnectionWorker_ProgressChanged;
if (_netConnectionWorker.IsBusy != true)
{
_netConnectionWorker.RunWorkerAsync();
}
}
public static void DisposeAsync()
{
StopAndWaitAsync();
_netConnectionWorker.ProgressChanged -= NetConnectionWorker_ProgressChanged;
_netConnectionWorker.RunWorkerCompleted -= NetConnectionWorker_RunWorkerCompleted;
_netConnectionWorker.DoWork -= NetConnectionWorker_DoWork;
_netConnectionWorker.Dispose();
}
private static void NetConnectionWorker_DoWork(object sender, DoWorkEventArgs e)
{
BackgroundWorker worker = sender as BackgroundWorker;
while (true)
{
if (worker.CancellationPending)
{
e.Cancel = true;
break;
}
// Perform a time consuming operation and report progress.
System.Threading.Thread.Sleep(500);
Ping pingSender = new Ping();
bool netAccess = true, slAccess = true;
try
{
PingReply reply = pingSender.Send(Site1);
if (reply.Status != IPStatus.Success)
{
netAccess = false;
}
}
catch (PingException ex)
{
netAccess = false;
Debug.WriteLine(ex.ToString());
}
try
{
PingReply reply = pingSender.Send(Site2);
if (reply.Status != IPStatus.Success)
{
slAccess = false;
}
}
catch (PingException ex)
{
slAccess = false;
Debug.WriteLine(ex.ToString());
}
if (!netAccess && !slAccess)
{
worker.ReportProgress(0);
}
if (!netAccess && slAccess)
{
worker.ReportProgress(1);
}
if (netAccess && !slAccess)
{
worker.ReportProgress(2);
}
if (netAccess && slAccess)
{
worker.ReportProgress(3);
}
System.Threading.Thread.Sleep(2000);
}
}
// This event handler updates the progress.
private static void NetConnectionWorker_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
switch (e.ProgressPercentage)
{
case 0:
_img1.Source = SetImgNetConn(false);
_img2.Source = SetImgNetConn(false);
break;
case 1:
_img1.Source = SetImgNetConn(false);
_img2.Source = SetImgNetConn(true);
break;
case 2:
_img1.Source = SetImgNetConn(true);
_img2.Source = SetImgNetConn(false);
break;
case 3:
_img1.Source = SetImgNetConn(true);
_img2.Source = SetImgNetConn(true);
break;
}
}
private static void NetConnectionWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
if (e.Cancelled)
{
Debug.WriteLine("Canceled!");
}
else if (e.Error != null)
{
Debug.WriteLine("Error: " + e.Error.Message);
}
else
{
Debug.WriteLine("Done!");
}
}
private static void StopAndWaitAsync()
{
if (_netConnectionWorker.WorkerSupportsCancellation)
{
_netConnectionWorker.CancelAsync();
while (_netConnectionWorker.IsBusy)
{
System.Threading.Thread.Sleep(500);
Application.DoEvents();
}
}
}
private static BitmapImage SetImgNetConn(bool notBusy)
{
string imgPath;
if (notBusy)
{
using (Bitmap bit = RABot.Properties.Resources.notBusy)
{
//метод просто выгружает указанную картинку в папку temp и возвращает полный путь к ней
imgPath = RaBotProgram.UnloadImage(bit, "notBusy");
}
}
else
{
using (Bitmap bit = RABot.Properties.Resources.busy)
{
imgPath = RaBotProgram.UnloadImage(bit, "busy");
}
}
var stream = File.OpenRead(imgPath);
BitmapImage b = new BitmapImage();
b.BeginInit();
b.CacheOption = BitmapCacheOption.OnLoad;
b.StreamSource = stream;
b.EndInit();
stream.Close();
return b;
}
}