代码优化:C# 中的带宽监控问题
我的问题主要是关于代码优化(目前) 我创建了一个网络监视器来监视PC上的不同连接,我所做的是在堆栈的第三层(网络层)嗅探数据包,捕获数据包后,我应该在上创建一个对象每个连接的用户界面,我现在正在做的是查看程序运行时每秒消耗的总带宽和发送的总数据量。这是代码的一部分:
temp= packet_rtxt.TextLength;
tempdr = temp / 1024;
dr_txt.Text=tempdr.ToString();
totaldata = totaldata + temp;
totaldatadisp = totaldata;
packet_rtxt.Text = "";
//unit
if (totaldata < 10485760)
{
if (totaldata < 10240)
unit.Text = "bytes";
else
{
totaldatadisp = totaldatadisp / 1024;
unit.Text = "KBs";
}
}
else
{
totaldata = totaldatadisp / 1048576;
unit.Text = "MBs";
}
test.Text = totaldatadisp.ToString();
tds.Enabled = true;
}
所以到目前为止,我所做的是将捕获的数据包写入富文本框中,获取该 rtxt 的长度并将其添加到总数据的计数器中,获取长度并将其用作数据速率,然后清除 rtxt 以获取下一位数据。 接收到的总数据部分工作正常,但是 BPs 部分对于少量数据工作正常,然后如果数据速率超过 10kbps(在我的电脑上),它就会变得疯狂 我应该尝试优化整个代码,还是有其他方法(请记住我需要监视每个连接),或者我是否需要使用不同的 UI 控件? 我应该专注于优化还是使用新方法?
提前致谢
my question is mainly about code optimization(at the moment)
I have created a network monitor that monitors different connections on the PC, what i had done is I'm sniffing packets at the 3rd level of the stack(the network level), after capturing the packet, i am supposed to create an object on the UI for each connection, what i am doing at the moment is looking at the overall consumed bandwidth and total data sent every second the program is run. here is that part of the code:
temp= packet_rtxt.TextLength;
tempdr = temp / 1024;
dr_txt.Text=tempdr.ToString();
totaldata = totaldata + temp;
totaldatadisp = totaldata;
packet_rtxt.Text = "";
//unit
if (totaldata < 10485760)
{
if (totaldata < 10240)
unit.Text = "bytes";
else
{
totaldatadisp = totaldatadisp / 1024;
unit.Text = "KBs";
}
}
else
{
totaldata = totaldatadisp / 1048576;
unit.Text = "MBs";
}
test.Text = totaldatadisp.ToString();
tds.Enabled = true;
}
so what im doing so far is writing out the captured packets into a rich text box, taking the length of that rtxt and adding it to a counter for the total data, taking the length and using it as the data rate, then clearing the rtxt for the next bits of data.
the total data recieved part is working fine, however the BPs section works fine for low amounts of data, then it goes crazy if the data rate is over 10kbps(on my pc)
should i try optimizing the whole code, or is there some other method(keep in mind i need to monitor every single connection), or do i need to use different UI controls?
should I focus on optimization or using new ways?
thanks in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
标准控制装置不适用于此类负载。您需要将数据记录与数据显示分开。
我每秒只显示最后一次 10kb 的文本。您仍然可以将所有日志记录保存在某些数据结构中。但您不必将它们全部推送到 UI。
或者,您可以编写自己的文本显示控件,但这将需要更多工作。
The standard controls are not made for such a load. You need to separate the logging of data from the display of data.
I'd only show the last say 10kb of text once per second. You can still keep all of the log records in some data structure. But you don't have to push all of them to the UI.
Alternatively you can write your own text-display control but that is going to be a lot more work.