I am using Pcapdot.net DLLs in order to send buffer of packets to my network adapter. I checked my Wireshark file and collected all the information e.g number of packets, duration etc..
My main class sends the buffer and has several properties (number of packets, duration..) In 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 Wireshark file has a timstamp and of course I can send this buffer in other speed rate by increasing or decreasing the timestamp between the packets.
now my problem is:
One of the properties I am checking is how many packets I have already sent. Because I know how many packets my file contains, I can show the progress via progress bar. If I am changing the send rate to maximum by deleting all the delay between the packets play, the speed is so fast and the progress bar and all the UI stuck until it’s finished sending all the packets. How can I change it? Maybe update my ProgressBar via another thread?
this is my function who check 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)
}
I don’t know Pcapdot.net, but I know Winforms so I can give you a couple of hints.
It appears that your UI is stuck in the sense that the window is unresponsive (you cannot click active buttons or edit textboxes, and if you try the window becomes gray). In that case you do have a threading problem. I don’t know if that
BackgroundWorkeris a library class or one that you made, but you must be sure that any I/O operation is done in another thread. While the name suggests that it happens, I’m not sure about how you start the process against what is supposed by the API. For example, certain libraries allow you to call astartmethod that forks another thread, while others require you to call anexecutemethod from a thread you already forked. This makes the difference.In order to update your UI from a non-UI thread you must always use
[Control.Invoke][1]method (if you need to pause your thread until UI gets updated) orControl.BeginInvoke(if you want to proceed ASAP).My
Lambda-fuis not trained daily, so please correct me if I make an error in my syntaxIn the second case in which your UI is stuck in the sense that the progress bar won’t progress, first make sure that the event is fired. Second and most important, you cannot update UI from a non-UI thread. Imperative!If you run that
progressChangedmethod inside a try-catch I see troubles, because when you update the progressBar percentage you get an exception that could be masked. I also hope thatProcessPercentageis an integer between 0 and 100 and not adoublebetween 0 and 1, something that causes the bar not to apparently progress.