Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Linq;
- using System.Net.NetworkInformation;
- using System.Text;
- using System.Threading.Tasks;
- using System.Threading;
- namespace SnapBoxProject
- {
- public class NetworkStatus : INotifyPropertyChanged
- {
- public event PropertyChangedEventHandler PropertyChanged = (sender, e) =>
- {
- };
- public string Network_Status { set; get; } = "Checking...";
- public string Network_Status_Icon { set; get; } = "";
- public string Network_Status_Color { set; get; } = "";
- public long Network_Send { set; get; } = 0;
- public long Network_Receive { set; get; } = 0;
- public NetworkStatus()
- {
- #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.
- try
- {
- Task.Factory.StartNew(() =>
- {
- do
- {
- NetworkInterface netInterfaces = NetworkInterface.GetAllNetworkInterfaces()[0];
- long BytesReceivedPerSecond = 0;
- long BytesSendPerSecond = 0;
- foreach (NetworkInterface netInterface in NetworkInterface.GetAllNetworkInterfaces())
- {
- if (netInterface.OperationalStatus == OperationalStatus.Up && netInterface.NetworkInterfaceType != NetworkInterfaceType.Loopback)
- {
- //MessageBox.Show(netInterface.NetworkInterfaceType.ToString());
- if (netInterface.NetworkInterfaceType.ToString().Contains("Ethernet"))
- {
- //Task newTask = Task.Run(() =>
- //{
- long BytesReceived_before = netInterface.GetIPv4Statistics().BytesReceived;
- long BytesSent_before = netInterface.GetIPv4Statistics().BytesSent;
- //Task.Delay(1000).Wait();
- Thread.Sleep(1000);
- long BytesReceived_after = netInterface.GetIPv4Statistics().BytesReceived;
- long BytesSent_after = netInterface.GetIPv4Statistics().BytesSent;
- BytesReceivedPerSecond = (BytesReceived_after - BytesReceived_before);
- BytesSendPerSecond = (BytesSent_after - BytesSent_before);
- //});
- //newTask.Wait();
- }
- else
- {
- long BytesReceived_before = netInterface.GetIPv4Statistics().BytesReceived;
- long BytesSent_before = netInterface.GetIPv4Statistics().BytesSent;
- //Task.Delay(1000).Wait();
- Thread.Sleep(1000);
- long BytesReceived_after = netInterface.GetIPv4Statistics().BytesReceived;
- long BytesSent_after = netInterface.GetIPv4Statistics().BytesSent;
- BytesReceivedPerSecond = (BytesReceived_after - BytesReceived_before);
- BytesSendPerSecond = (BytesSent_after - BytesSent_before);
- }
- }
- }
- string Connection_Status = CheckConnection();
- if (Connection_Status.Contains("Wireless"))
- {
- Network_Status = "Connected";
- Network_Status_Icon = "Wifi";
- Network_Status_Color = "Green";
- Network_Send = BytesSendPerSecond;
- Network_Receive = BytesReceivedPerSecond;
- }
- else if (Connection_Status.Contains("Ethernet"))
- {
- Network_Status = "Connected";
- Network_Status_Icon = "EthernetCable";
- Network_Status_Color = "Green";
- Network_Send = BytesSendPerSecond;
- Network_Receive = BytesReceivedPerSecond;
- }
- else if (Connection_Status.Contains("Disconnect"))
- {
- Network_Status = "Disconnect";
- Network_Status_Icon = "NetworkOffOutline";
- Network_Status_Color = "Red";
- }
- PropertyChanged(this, new PropertyChangedEventArgs("Network_Status"));
- PropertyChanged(this, new PropertyChangedEventArgs("Network_Status_Icon"));
- PropertyChanged(this, new PropertyChangedEventArgs("Network_Status_Color"));
- PropertyChanged(this, new PropertyChangedEventArgs("Network_Send"));
- PropertyChanged(this, new PropertyChangedEventArgs("Network_Receive"));
- //CheckConnection();
- }
- while (true);
- }, TaskCreationOptions.LongRunning);
- // Thread newThread = new Thread(() =>
- //#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.
- // {
- // do
- // {
- // NetworkInterface netInterfaces = NetworkInterface.GetAllNetworkInterfaces()[0];
- // long BytesReceivedPerSecond = 0;
- // long BytesSendPerSecond = 0;
- // foreach (NetworkInterface netInterface in NetworkInterface.GetAllNetworkInterfaces())
- // {
- // if (netInterface.OperationalStatus == OperationalStatus.Up && netInterface.NetworkInterfaceType != NetworkInterfaceType.Loopback)
- // {
- // //MessageBox.Show(netInterface.NetworkInterfaceType.ToString());
- // if (netInterface.NetworkInterfaceType.ToString().Contains("Ethernet"))
- // {
- // //Task newTask = Task.Run(() =>
- // //{
- // long BytesReceived_before = netInterface.GetIPv4Statistics().BytesReceived;
- // long BytesSent_before = netInterface.GetIPv4Statistics().BytesSent;
- // //Task.Delay(1000).Wait();
- // Thread.Sleep(1000);
- // long BytesReceived_after = netInterface.GetIPv4Statistics().BytesReceived;
- // long BytesSent_after = netInterface.GetIPv4Statistics().BytesSent;
- // BytesReceivedPerSecond = (BytesReceived_after - BytesReceived_before);
- // BytesSendPerSecond = (BytesSent_after - BytesSent_before);
- // //});
- // //newTask.Wait();
- // }
- // else
- // {
- // long BytesReceived_before = netInterface.GetIPv4Statistics().BytesReceived;
- // long BytesSent_before = netInterface.GetIPv4Statistics().BytesSent;
- // //Task.Delay(1000).Wait();
- // Thread.Sleep(1000);
- // long BytesReceived_after = netInterface.GetIPv4Statistics().BytesReceived;
- // long BytesSent_after = netInterface.GetIPv4Statistics().BytesSent;
- // BytesReceivedPerSecond = (BytesReceived_after - BytesReceived_before);
- // BytesSendPerSecond = (BytesSent_after - BytesSent_before);
- // }
- // }
- // }
- // string Connection_Status = CheckConnection();
- // if (Connection_Status.Contains("Wireless"))
- // {
- // Network_Status = "Connected";
- // Network_Status_Icon = "Wifi";
- // Network_Status_Color = "Green";
- // Network_Send = BytesSendPerSecond;
- // Network_Receive = BytesReceivedPerSecond;
- // }
- // else if (Connection_Status.Contains("Ethernet"))
- // {
- // Network_Status = "Connected";
- // Network_Status_Icon = "EthernetCable";
- // Network_Status_Color = "Green";
- // Network_Send = BytesSendPerSecond;
- // Network_Receive = BytesReceivedPerSecond;
- // }
- // else if (Connection_Status.Contains("Disconnect"))
- // {
- // Network_Status = "Disconnect";
- // Network_Status_Icon = "NetworkOffOutline";
- // Network_Status_Color = "Red";
- // }
- // PropertyChanged(this, new PropertyChangedEventArgs("Network_Status"));
- // PropertyChanged(this, new PropertyChangedEventArgs("Network_Status_Icon"));
- // PropertyChanged(this, new PropertyChangedEventArgs("Network_Status_Color"));
- // PropertyChanged(this, new PropertyChangedEventArgs("Network_Send"));
- // PropertyChanged(this, new PropertyChangedEventArgs("Network_Receive"));
- // //CheckConnection();
- // }
- // while (true);
- // });
- // newThread.IsBackground = true;
- // newThread.Priority = ThreadPriority.AboveNormal;
- // newThread.Start();
- }
- catch
- {
- }
- }
- private string CheckConnection()
- {
- //foreach (NetworkInterface netInterface in NetworkInterface.GetAllNetworkInterfaces())
- //{
- NetworkInterface netInterfaces = NetworkInterface.GetAllNetworkInterfaces()[0];
- foreach (NetworkInterface netInterface in NetworkInterface.GetAllNetworkInterfaces())
- {
- if (netInterface.OperationalStatus == OperationalStatus.Up && netInterface.NetworkInterfaceType != NetworkInterfaceType.Loopback)
- {
- //MessageBox.Show(netInterface.NetworkInterfaceType.ToString());
- if (netInterface.NetworkInterfaceType.ToString().Contains("Ethernet"))
- return "Ethernet";
- if (netInterface.NetworkInterfaceType.ToString().Contains("Wireless"))
- return "Wireless";
- }
- }
- return "Disconnect";
- //}
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement