简体   繁体   English

代码优化:C#问题中的带宽监视器

[英]Code optimization: bandwidth monitor in c# issues

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. 我的问题主要是关于代码优化(目前),我创建了一个网络监视器来监视PC上的不同连接,我所做的是我在堆栈的第3级(网络级)嗅探数据包,之后捕获数据包后,我应该在UI上为每个连接创建一个对象,此刻我正在做的事情是查看总消耗的带宽以及程序运行每秒发送的总数据。 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. 所以到目前为止,我所做的就是将捕获的数据包写到一个富文本框中,获取该rtxt的长度并将其添加到总数据计数器中,获取该长度并将其用作数据速率,然后清除rtxt用于接下来的数据位。 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? 接收到的总数据部分工作正常,但是BPs部分适用于少量数据,如果我尝试优化整个代码,或者如果数据速率超过10kbps(在我的电脑上),那就太疯狂了方法(请记住,我需要监视每个连接),还是需要使用其他UI控件? should I focus on optimization or using new ways? 我应该专注于优化还是使用新方法?

thanks in advance 提前致谢

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. 我只会每秒显示一次最后说出的10kb文本。 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. 但是您不必将所有控件都推送到UI。

Alternatively you can write your own text-display control but that is going to be a lot more work. 另外,您可以编写自己的文本显示控件,但这将需要更多的工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM