简体   繁体   中英

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. 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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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