Reputation: 531
I have a multithreaded program. I want start the new BackgroundWorker
and pause the current thread. Then I want resume previous thread in the new BackgroundWorker
.
I am programming in C#.
I have a big project and cannot put my code here.
Upvotes: 2
Views: 471
Reputation: 3965
Here is my sample code for you! I'm not so sure it useful for your project, but this is my idea. hope helpful.
BackgroundWorker bwExportLogFile = new BackgroundWorker();
private void ExportLogFile() {
bwExportLogFile.DoWork += bwExportLogFile_DoWork;
bwExportLogFile.RunWorkerCompleted += bwExportLogFile_RunWorkerCompleted;
bwExportLogFile.ProgressChanged += bwExportLogFile_ProgressChanged;
bwExportLogFile.RunWorkerAsync();
bwExportLogFile.WorkerReportsProgress = true;
bwExportLogFile.WorkerSupportsCancellation = true;
}
void bwExportLogFile_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
BackgroundWorker bw = sender as BackgroundWorker;
if(some thing is true here){
bw.CancelAsync();
}
}
So when you want to run thread in BackgroundWorker again just call this:
bwExportLogFile.RunWorkerAsync();
Upvotes: 0
Reputation: 3965
Try to set WorkerSupportsCancellation = true and in ProgressChanged event you can do like this:
BackgroundWorker bw = sender as BackgroundWorker;
bw.CancelAsync();
bw.RunWorkerAsync();
Upvotes: 0
Reputation: 148110
You can use AutoResetEvent and use WaitOne to hold the parent thread. Call AutoResetEvent.Set method from spawned thread to resume the execution of parent (main) thread.
childThread.Start();
autoResetEvent.WaitOne();
In child (spawned thread)
private void SpawnedThread()
{
//your code
autoResetEvent.Set(); //will resume the execution after WaitOne(), may be under some condition.
}
You can use overloaded version of WaitOne to give the maximum wait time. The execution will resume of Set method is not being called until the give time.
Upvotes: 1