i am use my Winform application to send buffer of packet to my network card, my application show to progress via Progress Bar, if i choose to send all the buffer with out any delay between the packets my application stuck (become gray) and return to normal behaviour only after senging all the buffer (in case my buffer is very big, something like > 50,000 packets)
so how can i deal with such case ? maybee to change the backgroundworker who check all my class properties (number of packet send etc…) with event ?
listBoxFiles.SetSelected(0, true);
bgWoSingle = new BackgroundWorker();
bgWoSingle.WorkerReportsProgress = true;
bgWoSingle.ProgressChanged += new ProgressChangedEventHandler(bgW_ProgressChanged);
bgWoSingle.DoWork += new DoWorkEventHandler(
(s3, e3) =>
{
while (loopsCount < numberOfLoops && bContinuePlay && ifContinue)
{
for (int i = 0; (i < listBoxFiles.Items.Count) && bContinuePlay && ifContinue; i++)
{
this.Invoke((MethodInvoker)delegate
{
lbCurrentFileNum.Text = "(" + (i + 1) + "/" + listBoxFiles.Items.Count + "):";
});
string path = (string)listBoxFiles.Items[i];
pcap = new Pcap(path, playSpeed, isSync);
pcap._startTimer += new EventHandler(pcap_packetStartTimer);
pcap._stopTimer += new EventHandler(pcap__packetStopTimer);
pcap.evePacketProgress += new Pcap.dlgPacketProgress(
(progressCount) =>
{
pcap._fileSelectedIndex = i;
bgWoSingle.ReportProgress(progressCount, pcap);
});
if (selectedAdapter != null)
{
//play the file
bContinuePlay = pcap.playCapture(selectedAdapter._packetDevice); }
}
loopsCount++;
}
bgWoSingle.RunWorkerCompleted += new RunWorkerCompletedEventHandler(
(s3, e3) =>
{
}
);
bgWoSingle.RunWorkerAsync();
here i check the class properties (class name is pcap):
void bgW_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
//check the properties and update the UI
labelPacketSent.Text = pcap._numberOfSendPackets.ToString("#,##0");
progressBar1.Value = e.ProgressPercentage;
}
You have to use BackGroundWorker for it. I am adding a small example for it.
The worker can send the percentage of completion or any other data you want through UserState.