C# BackgroundWorker skips DoWork and goes straight to RunWorkerCompleted

Posted by mr_man on Stack Overflow See other posts from Stack Overflow or by mr_man
Published on 2012-11-22T22:43:59Z Indexed on 2012/11/22 22:59 UTC
Read the original article Hit count: 452

Filed under:
|
|

I'm new with C# so go easy on me!

So far - I have a console app that listens for client connections and replies to the client accordingly.

I also have a WPF form with a button and a text box. The button launches some code to connect to the server as a BackgroundWorker, which then waits for a response before appending it to the end of the text box.

This works great, once. Sometimes twice. But then it kept crashing - turns out that the DoWork block wasn't being called at all and it was going straight to RunWorkerCompleted. Of course, the .result is blank so trying to convert it to a string fails.

Is this a rookie mistake? I have tried searching the internet for various ways of saying the above but haven't come across anything useful...

This is the code so far: http://pastebin.com/ZQvCFqxN - there are so many debug outputs from me trying to figure out exactly what went wrong.

This is the result of the debug outputs: http://pastebin.com/V412mppX

Any help much appreciated. Thanks!

EDIT: The relevant code post-fix (thanks to Patrick Quirk below) is:

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

© Stack Overflow or respective owner

Related posts about c#

Related posts about .NET