lundi 21 avril 2014

c# - puis-je ajouter un rappel à un BackgroundWorker qui est déjà en cours d'exécution ? -Débordement de pile


Is it possible to add a callback to a background worker while it is running ?


bw.DoWork += new DoWorkEventHandler( some callback );

bw.RunWorkerAsync();

bw.DoWork += new DoWorkEventHandler( some callback );

Thank you.




Yes you can as it's only a subscription to an event but you can't run bw until he has completed the execution of the first task


here an example to illustrate this the following code will show an InvalidOperationException telling This BackgroundWorker is currently busy and cannot run multiple tasks concurrently."


public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
backgroundWorker1.DoWork += new DoWorkEventHandler(backgroundWorker1_DoWork);
backgroundWorker1.RunWorkerAsync();
backgroundWorker1.DoWork+=new DoWorkEventHandler(backgroundWorker2_DoWork);
//at this line you get an InvalidOperationException
backgroundWorker1.RunWorkerAsync();




}

void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
do
{

} while (true);
}
void backgroundWorker2_DoWork(object sender, DoWorkEventArgs e)
{
do
{

} while (true);
}
}

As an answer to your comment question


@SriramSakthivel Thanks. Is there a way to put tasks in a queue ?


yes you can if you are using .net 4.0 you can use task with ContinueWith and attach it to your UI taskScheduler it will have the same behavior as if you are using BackgroundWorker


private void TestButton_Click(object sender, EventArgs e)
{
TestButton.Enabled = false;
var uiThreadScheduler = TaskScheduler.FromCurrentSynchronizationContext();

var backgroundTask = new Task(() =>
{
Thread.Sleep(5000);
});

var uiTask = backgroundTask.ContinueWith(t =>
{
TestButton.Enabled = true;
}, uiThreadScheduler);

backgroundTask.Start();
}


Is it possible to add a callback to a background worker while it is running ?


bw.DoWork += new DoWorkEventHandler( some callback );

bw.RunWorkerAsync();

bw.DoWork += new DoWorkEventHandler( some callback );

Thank you.



Yes you can as it's only a subscription to an event but you can't run bw until he has completed the execution of the first task


here an example to illustrate this the following code will show an InvalidOperationException telling This BackgroundWorker is currently busy and cannot run multiple tasks concurrently."


public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
backgroundWorker1.DoWork += new DoWorkEventHandler(backgroundWorker1_DoWork);
backgroundWorker1.RunWorkerAsync();
backgroundWorker1.DoWork+=new DoWorkEventHandler(backgroundWorker2_DoWork);
//at this line you get an InvalidOperationException
backgroundWorker1.RunWorkerAsync();




}

void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
do
{

} while (true);
}
void backgroundWorker2_DoWork(object sender, DoWorkEventArgs e)
{
do
{

} while (true);
}
}

As an answer to your comment question


@SriramSakthivel Thanks. Is there a way to put tasks in a queue ?


yes you can if you are using .net 4.0 you can use task with ContinueWith and attach it to your UI taskScheduler it will have the same behavior as if you are using BackgroundWorker


private void TestButton_Click(object sender, EventArgs e)
{
TestButton.Enabled = false;
var uiThreadScheduler = TaskScheduler.FromCurrentSynchronizationContext();

var backgroundTask = new Task(() =>
{
Thread.Sleep(5000);
});

var uiTask = backgroundTask.ContinueWith(t =>
{
TestButton.Enabled = true;
}, uiThreadScheduler);

backgroundTask.Start();
}

0 commentaires:

Enregistrer un commentaire