0

I have developed an application which one capture network card speeds,i use below code to get data:

NetworkInterface nic = nicArr[0];
IPv4InterfaceStatistics interfaceStats = nic.GetIPv4Statistics();
Int64 bytesSentSpeed = (Int64)(interfaceStats.BytesSent - uploadSpeed) / 1024;
uploadSpeed = double.Parse(interfaceStats.BytesSent.ToString());
Int64 bytesReceivedSpeed = (int)(interfaceStats.BytesReceived - downloadSpeed) / 1024;
downloadSpeed = double.Parse(interfaceStats.BytesReceived.ToString());
UpDownSpeed.Add("UpSpeed", bytesSentSpeed.ToString());
UpDownSpeed.Add("DownSpeed", bytesReceivedSpeed.ToString());

and then i get value:

UpDpwnSpeed["UpSpeed"].ToString()

why i get a number -2097152?

zhang mike
  • 65
  • 1
  • 9
  • 1
    looks like an over flow exception. But you are not showing the whole picture: the definitions for `uploadSpeed` and `downloadSpeed` are missing, and is this inside a while loop? – kennyzx May 22 '17 at 10:20
  • It's not inside a loop.It's an over flow exception problem,change int to Int64,i resolve this problem,thank you! – zhang mike May 24 '17 at 02:15

1 Answers1

0

It seems you have uploadSpeed and downloadSpeed as float, for bytesReceivedSpeed you cast the expression to to int, so if you have the value in (interfaceStats.BytesReceived - downloadSpeed) bigger as 2147483647(int.MaxValue), then you become a negative value on down cast (int).
Make all operations as float and just format value for your dictionary.

Rekshino
  • 6,954
  • 2
  • 19
  • 44