UI stuck while updating ProgressBar

I am using Pcapdot.net libraries to send a packet buffer to my network adapter. I checked the Wireshark file and gathered all the information like packet count, duration, etc. My main class sends a buffer and has several properties (number of packets, duration ..). On the main thread, I am checking this class with BackgroundWorker.ProgressChanged:

            bgWoSingle = new BackgroundWorker();
            bgWoSingle.WorkerReportsProgress = true;
bgWoSingle.ProgressChanged += new ProgressChangedEventHandler(bgW_ProgressChanged);

      

Every packet inside the Wireshark file is timestamped and of course I can send that buffer at a different rate by increasing or decreasing the timestamp between packets. now my problem is:

One of the properties I check is how many packets I have already sent. Since I know how many packages my file contains, I can show the progress through the progress bar. If I change the send rate to the maximum, removing all the delay between playing packets, the rate is just as fast, and the progress bar and the entire UI are stuck until it finishes sending all packets. How can I change it? Maybe update my ProgressBar via a different thread?

this is my function that checks my class:

  void bgW_ProgressChanged(object sender, ProgressChangedEventArgs e)
  {
    //bla bla bla (check all Class properties)

        pcap = e.UserState as Pcap;
        progressBar1.Value = e.ProgressPercentage; //here is my progressBar update


    //bla bla bla (check all Class properties)
  }

      

+2


source to share


2 answers


I don't know Pcapdot.net, but I know Winforms, so I can give you a couple of tips.

It looks like your UI is getting stuck in the sense that the window is unresponsive (you can't click active buttons or edit text boxes, and if you try, the window will go gray). In this case, you have a threading problem. I don't know if this is BackgroundWorker

the library class or the one you created, but you have to make sure that any I / O operation is done on a different thread. While the title suggests this is happening, I'm not sure how you start the process with what the API is supposed to be. For example, some libraries allow you to call a method start

that calls another thread, while others require you to call a method execute

from a thread that you have already forked. It makes a difference.

To update your UI from a non-UI thread, you should always use the method [Control.Invoke][1]

(if you need to pause the thread until the UI is updated) or Control.BeginInvoke

(if you want to continue as soon as possible).

Mine Lambda-fu

doesn't train daily so please correct me if I make a syntax error



progressBar1.Invoke(delegate() {progressBar1.Value = e.ProgressPercentage;});

      

In the second case, when your UI gets stuck in the sense that the progress bar won't progress, make sure the event is fired first. Second, and most importantly, you cannot update the UI from a non -UI thread. Imperative!

If you run this method progressChanged

inside try-catch I see problems because when you update the progressBar percentage you get an exception that can be masked. I also hope it ProcessPercentage

is an integer between 0 and 100, not double

between 0 and 1, which causes the bar to clearly not progress.

+1


source


You are just calling ReportProgress () too often. So often that the UI thread is flooded with call requests and no longer bypasses its normal responsibilities. How to draw and react to input events. Because when it is ready to execute the next notification there will be another request to call the handler for the ProgressChanged event, it will be the first.



You will need to slow it down at a reasonable rate. Reasonable is easy to define, you are only doing it for the human eye. Which sees nothing but blur when you do it faster than 20 times per second. You've passed it right now, perhaps over a thousand times a second. One easy way is to just count 50 records before you call ReportProgress. Adjust if necessary.

+6


source







All Articles