question

maxstell-8006 avatar image
0 Votes"
maxstell-8006 asked lextm commented

This BackgroundWorker is currently busy and cannot run multiple tasks concurrently

Help Please How I can Run Multiple task And How To Add A button To Stop The Search and in the same time stop the BackgroundWorker

239654-error.png




the code :
public partial class Form1 : Form
{
private Searcher searcher;
public Form1()
{
InitializeComponent();
this.searcher = new Searcher(null, null,null);
this.searcher.onFileFound += FileFound;
bgWorker.DoWork += WorkInBackground;
bgWorker.RunWorkerCompleted += WorkerCompleted;
}
private void FileFound(string path)
{
listBox1.BeginInvoke((Action)delegate ()
{
listBox1.Items.Add(path);
});
}
private void WorkerCompleted(object sender, RunWorkerCompletedEventArgs args)
{
textBox3.Text = listBox1.Items.Count.ToString();
MessageBox.Show("Search is Done");
}
private void WorkInBackground(object sender, DoWorkEventArgs args)
{
searcher.Search();
}

dotnet-csharpdotnet-visual-basicwindows-forms
error.png (24.5 KiB)
· 5
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Consider placing your project into a GitHub repository (do not zip) and provide the link in your question, not a comment or answer. That is easier than posting partial code for those here to assist.

0 Votes 0 ·

public partial class Form1 : Form
{
private Searcher searcher;
public Form1()
{
InitializeComponent();
this.searcher = new Searcher(null, null,null);
this.searcher.onFileFound += FileFound;
bgWorker.DoWork += WorkInBackground;
bgWorker.RunWorkerCompleted += WorkerCompleted;
}
private void FileFound(string path)
{
listBox1.BeginInvoke((Action)delegate ()
{
listBox1.Items.Add(path);
});
}
private void WorkerCompleted(object sender, RunWorkerCompletedEventArgs args)
{
textBox3.Text = listBox1.Items.Count.ToString();
MessageBox.Show("Search is Done");
}
private void WorkInBackground(object sender, DoWorkEventArgs args)
{
searcher.Search();
}

0 Votes 0 ·

Not what I asked.

0 Votes 0 ·

@maxstell-8006, Welcome to Microsoft Q&A, Could you provide the related code about your Searcher class or provide the related reference about it? It will help us to analyze your problem.

0 Votes 0 ·

BackgroundWorker is the ancient approach which no one should use any more now. Try to replace it with async/await https://halfblood.pro/how-to-replace-backgroundworker-with-async-await-and-tasks-80d7c8ed89dc and then such issues are gone.

0 Votes 0 ·

0 Answers