C# BackgroundWorker Woes

Status
Not open for further replies.

Sharky

Community Paragon
Community Support
Messages
4,399
Reaction score
94
Points
48
OK so I'm not a pro at C#... I've managed to put together some code that runs in a BackgroundWorker process, and it works great. Once.

On the second launch, it goes straight to the RunWorkerCompleted code, which fails because .result isn't defined.

Any suggestions?

First run works correctly. Second and subsequent runs don't bother with the DoWork code (I put some random Debug.WriteLine commands in there that don't fire) and instead it just breaks on error on the red line, below. Error message is 'Object reference not set to an instance of an object.'

Code:
  public void dorequest(string query)
  {
   request = new BackgroundWorker();
   request.WorkerSupportsCancellation = true;
   request.WorkerReportsProgress = true;
   request.RunWorkerAsync(query);
   request.DoWork += new DoWorkEventHandler(request_DoWork);
   request.ProgressChanged += new ProgressChangedEventHandler(request_ProgressChanged);
   request.RunWorkerCompleted += new RunWorkerCompletedEventHandler(request_RunWorkerCompleted); 
  }

EDIT: It's fixed - the async line needs to be at the end. Also at http://stackoverflow.com/questions/...owork-and-goes-straight-to-runworkercompleted .
 
Last edited:
Status
Not open for further replies.
Top