Advertisement
Guest User

Untitled

a guest
Dec 9th, 2019
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.93 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Linq;
  5. using System.Net.NetworkInformation;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. using System.Threading;
  9. namespace SnapBoxProject
  10. {
  11. public class NetworkStatus : INotifyPropertyChanged
  12. {
  13. public event PropertyChangedEventHandler PropertyChanged = (sender, e) =>
  14. {
  15.  
  16. };
  17.  
  18. public string Network_Status { set; get; } = "Checking...";
  19.  
  20. public string Network_Status_Icon { set; get; } = "";
  21.  
  22. public string Network_Status_Color { set; get; } = "";
  23.  
  24. public long Network_Send { set; get; } = 0;
  25.  
  26. public long Network_Receive { set; get; } = 0;
  27.  
  28.  
  29. public NetworkStatus()
  30. {
  31. #pragma warning disable CS1998 // This async method lacks 'await' operators and will run synchronously. Consider using the 'await' operator to await non-blocking API calls, or 'await Task.Run(...)' to do CPU-bound work on a background thread.
  32. try
  33. {
  34. Task.Factory.StartNew(() =>
  35. {
  36. do
  37. {
  38. NetworkInterface netInterfaces = NetworkInterface.GetAllNetworkInterfaces()[0];
  39. long BytesReceivedPerSecond = 0;
  40. long BytesSendPerSecond = 0;
  41. foreach (NetworkInterface netInterface in NetworkInterface.GetAllNetworkInterfaces())
  42. {
  43. if (netInterface.OperationalStatus == OperationalStatus.Up && netInterface.NetworkInterfaceType != NetworkInterfaceType.Loopback)
  44. {
  45. //MessageBox.Show(netInterface.NetworkInterfaceType.ToString());
  46. if (netInterface.NetworkInterfaceType.ToString().Contains("Ethernet"))
  47. {
  48. //Task newTask = Task.Run(() =>
  49. //{
  50. long BytesReceived_before = netInterface.GetIPv4Statistics().BytesReceived;
  51. long BytesSent_before = netInterface.GetIPv4Statistics().BytesSent;
  52.  
  53. //Task.Delay(1000).Wait();
  54. Thread.Sleep(1000);
  55. long BytesReceived_after = netInterface.GetIPv4Statistics().BytesReceived;
  56. long BytesSent_after = netInterface.GetIPv4Statistics().BytesSent;
  57.  
  58. BytesReceivedPerSecond = (BytesReceived_after - BytesReceived_before);
  59. BytesSendPerSecond = (BytesSent_after - BytesSent_before);
  60. //});
  61. //newTask.Wait();
  62.  
  63. }
  64. else
  65. {
  66. long BytesReceived_before = netInterface.GetIPv4Statistics().BytesReceived;
  67. long BytesSent_before = netInterface.GetIPv4Statistics().BytesSent;
  68.  
  69. //Task.Delay(1000).Wait();
  70. Thread.Sleep(1000);
  71. long BytesReceived_after = netInterface.GetIPv4Statistics().BytesReceived;
  72. long BytesSent_after = netInterface.GetIPv4Statistics().BytesSent;
  73.  
  74. BytesReceivedPerSecond = (BytesReceived_after - BytesReceived_before);
  75. BytesSendPerSecond = (BytesSent_after - BytesSent_before);
  76. }
  77. }
  78. }
  79.  
  80. string Connection_Status = CheckConnection();
  81. if (Connection_Status.Contains("Wireless"))
  82. {
  83. Network_Status = "Connected";
  84. Network_Status_Icon = "Wifi";
  85. Network_Status_Color = "Green";
  86. Network_Send = BytesSendPerSecond;
  87. Network_Receive = BytesReceivedPerSecond;
  88. }
  89. else if (Connection_Status.Contains("Ethernet"))
  90. {
  91. Network_Status = "Connected";
  92. Network_Status_Icon = "EthernetCable";
  93. Network_Status_Color = "Green";
  94. Network_Send = BytesSendPerSecond;
  95. Network_Receive = BytesReceivedPerSecond;
  96. }
  97. else if (Connection_Status.Contains("Disconnect"))
  98. {
  99. Network_Status = "Disconnect";
  100. Network_Status_Icon = "NetworkOffOutline";
  101. Network_Status_Color = "Red";
  102. }
  103. PropertyChanged(this, new PropertyChangedEventArgs("Network_Status"));
  104. PropertyChanged(this, new PropertyChangedEventArgs("Network_Status_Icon"));
  105. PropertyChanged(this, new PropertyChangedEventArgs("Network_Status_Color"));
  106. PropertyChanged(this, new PropertyChangedEventArgs("Network_Send"));
  107. PropertyChanged(this, new PropertyChangedEventArgs("Network_Receive"));
  108. //CheckConnection();
  109. }
  110. while (true);
  111. }, TaskCreationOptions.LongRunning);
  112.  
  113. // Thread newThread = new Thread(() =>
  114. //#pragma warning restore CS1998 // This async method lacks 'await' operators and will run synchronously. Consider using the 'await' operator to await non-blocking API calls, or 'await Task.Run(...)' to do CPU-bound work on a background thread.
  115. // {
  116. // do
  117. // {
  118. // NetworkInterface netInterfaces = NetworkInterface.GetAllNetworkInterfaces()[0];
  119. // long BytesReceivedPerSecond = 0;
  120. // long BytesSendPerSecond = 0;
  121. // foreach (NetworkInterface netInterface in NetworkInterface.GetAllNetworkInterfaces())
  122. // {
  123. // if (netInterface.OperationalStatus == OperationalStatus.Up && netInterface.NetworkInterfaceType != NetworkInterfaceType.Loopback)
  124. // {
  125. // //MessageBox.Show(netInterface.NetworkInterfaceType.ToString());
  126. // if (netInterface.NetworkInterfaceType.ToString().Contains("Ethernet"))
  127. // {
  128. // //Task newTask = Task.Run(() =>
  129. // //{
  130. // long BytesReceived_before = netInterface.GetIPv4Statistics().BytesReceived;
  131. // long BytesSent_before = netInterface.GetIPv4Statistics().BytesSent;
  132.  
  133. // //Task.Delay(1000).Wait();
  134. // Thread.Sleep(1000);
  135. // long BytesReceived_after = netInterface.GetIPv4Statistics().BytesReceived;
  136. // long BytesSent_after = netInterface.GetIPv4Statistics().BytesSent;
  137.  
  138. // BytesReceivedPerSecond = (BytesReceived_after - BytesReceived_before);
  139. // BytesSendPerSecond = (BytesSent_after - BytesSent_before);
  140. // //});
  141. // //newTask.Wait();
  142.  
  143. // }
  144. // else
  145. // {
  146. // long BytesReceived_before = netInterface.GetIPv4Statistics().BytesReceived;
  147. // long BytesSent_before = netInterface.GetIPv4Statistics().BytesSent;
  148.  
  149. // //Task.Delay(1000).Wait();
  150. // Thread.Sleep(1000);
  151. // long BytesReceived_after = netInterface.GetIPv4Statistics().BytesReceived;
  152. // long BytesSent_after = netInterface.GetIPv4Statistics().BytesSent;
  153.  
  154. // BytesReceivedPerSecond = (BytesReceived_after - BytesReceived_before);
  155. // BytesSendPerSecond = (BytesSent_after - BytesSent_before);
  156. // }
  157. // }
  158. // }
  159.  
  160. // string Connection_Status = CheckConnection();
  161. // if (Connection_Status.Contains("Wireless"))
  162. // {
  163. // Network_Status = "Connected";
  164. // Network_Status_Icon = "Wifi";
  165. // Network_Status_Color = "Green";
  166. // Network_Send = BytesSendPerSecond;
  167. // Network_Receive = BytesReceivedPerSecond;
  168. // }
  169. // else if (Connection_Status.Contains("Ethernet"))
  170. // {
  171. // Network_Status = "Connected";
  172. // Network_Status_Icon = "EthernetCable";
  173. // Network_Status_Color = "Green";
  174. // Network_Send = BytesSendPerSecond;
  175. // Network_Receive = BytesReceivedPerSecond;
  176. // }
  177. // else if (Connection_Status.Contains("Disconnect"))
  178. // {
  179. // Network_Status = "Disconnect";
  180. // Network_Status_Icon = "NetworkOffOutline";
  181. // Network_Status_Color = "Red";
  182. // }
  183. // PropertyChanged(this, new PropertyChangedEventArgs("Network_Status"));
  184. // PropertyChanged(this, new PropertyChangedEventArgs("Network_Status_Icon"));
  185. // PropertyChanged(this, new PropertyChangedEventArgs("Network_Status_Color"));
  186. // PropertyChanged(this, new PropertyChangedEventArgs("Network_Send"));
  187. // PropertyChanged(this, new PropertyChangedEventArgs("Network_Receive"));
  188. // //CheckConnection();
  189. // }
  190. // while (true);
  191. // });
  192.  
  193. // newThread.IsBackground = true;
  194. // newThread.Priority = ThreadPriority.AboveNormal;
  195. // newThread.Start();
  196. }
  197. catch
  198. {
  199.  
  200. }
  201.  
  202. }
  203. private string CheckConnection()
  204. {
  205. //foreach (NetworkInterface netInterface in NetworkInterface.GetAllNetworkInterfaces())
  206. //{
  207. NetworkInterface netInterfaces = NetworkInterface.GetAllNetworkInterfaces()[0];
  208. foreach (NetworkInterface netInterface in NetworkInterface.GetAllNetworkInterfaces())
  209. {
  210. if (netInterface.OperationalStatus == OperationalStatus.Up && netInterface.NetworkInterfaceType != NetworkInterfaceType.Loopback)
  211. {
  212. //MessageBox.Show(netInterface.NetworkInterfaceType.ToString());
  213. if (netInterface.NetworkInterfaceType.ToString().Contains("Ethernet"))
  214. return "Ethernet";
  215. if (netInterface.NetworkInterfaceType.ToString().Contains("Wireless"))
  216. return "Wireless";
  217. }
  218. }
  219. return "Disconnect";
  220.  
  221.  
  222. //}
  223. }
  224. }
  225. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement