Reputation: 4961
I have an application which i am add files to my Listbox
and run those files.
My application play this files using PcapDot.Net
project DLLs and send the packets through the network adapter.
The way is very simple: after all the files added to my application Listbox
and the play button clicked the application handle the first file and after this file finished the next file began.
what i want to do is add control to my GUI that control the number of open thread in order to have the ability to play several file simultaneous.
This is my play button event:
private BackgroundWorker bw;
private void btnPlay_Click(object sender, EventArgs e)
{
bw = new BackgroundWorker();
bw.WorkerReportsProgress = true;
bw.WorkerSupportsCancellation = true;
bw.DoWork += new DoWorkEventHandler(bw_DoWork);
bw.ProgressChanged += new ProgressChangedEventHandler(bw_ProgressChanged);
bw.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bw_RunWorkerCompleted);
manualResetEvent = new ManualResetEvent(false);
if (bw.IsBusy != true)
bw.RunWorkerAsync();
}
private void bw_DoWork(object sender, DoWorkEventArgs e)
{
for (int i = 0; i < listBoxFiles.Items.Counti++) //run in loop all over my listbox
{
// here if have several wiresharkFile events that update my UI:
wiresharkFile.statusChangedEvent += new WiresharkFile.StatusChangedDelegate(
(wFile) =>
{
bw.ReportProgress(wiresharkFile.packetProgressPrecentage, wiresharkFile);
});
wiresharkFile.sendBuffer(); //play the file
}
}
What is the best way to add option to open more than 1 thread in the same time ?
Upvotes: 1
Views: 139
Reputation: 11597
here is a simple example for your use, it shows how to create and sign to an event you'll pop when the thread that open a file ends and then you can, when the event pop, to open another file. make sure you keep a counter and a lock so you won't have race conditions
public delegate void FileClosedHndlr();
public class MyThread
{
private event FileClosedHndlr FileClosed;
public void MyMain()
{
Thread t = new Thread(new ThreadStart(start));
FileClosed += new FileClosedHndlr(MyThread_FileClosed);
t.Start();
}
void MyThread_FileClosed()
{
// Thread has ended file open
// open another file
}
private void start()
{
// Open the file
// End thread
if (FileClosed != null)
{
FileClosed();
}
}
}
it took me a while, so use it
Upvotes: 1