views:

37

answers:

2

Let's say I have a backgroundWorker1_DoWork that finds a value in a database and returns it in e.Result. Let's say I also have a backgroundWorker1_RunWorkerCompleted that reads the result.

What I want to be able to do is based on e.Result, I can re-run backgroundWorker1_DoWork, and this I am not sure how to do.

I'd really appreciate any help.

+2  A: 

Can't you restart your BackgroundWorker after testing e.Result in your backgroundWorker1_RunWorkerCompleted event?

Jay Riggs
I'm starting it off using backgroundWorker1.RunWorkerAsnc(); in the Form1() initiation. If I want to restart it as you posted, what code do I use and do I just put it in my backgroundWorker1_RunWorkerCompleted?
Soo
@Soo - use backgroundWorker1.RunWorkerAsync(); as Darin suggests. I assume this is how you started the background worker in the first place?
Jay Riggs
Works great!Now a quick little question, should I put the first RunWorkerAsync in Form1() or Form1_Load()?
Soo
I'd put it in Form1_Load.
Jay Riggs
Soo, either Form1() and Form1_Load() is ok.
Lex Li
@Lex Li - I beleive so. If you need to reference a control and you want to use Form1(), be sure to do so after the call to InitialiseComponent(). Before then the form's controls don't exist.
Jay Riggs
+2  A: 

The same way you are doing it the first time - by invoking the RunWorkerAsync method:

backgroundWorker1.RunWorkerAsync();

Make sure the invocation is conditioned or this loop may run forever :-)

Darin Dimitrov