Problems debugging BackgroundWorker and solution

Did you ever set a breakpoint within BackgroundWorker execution (worker thread), hit it and then application would stall. It is certainly a strange behavior.


Apparently the problem happens because debugger calls into ToString() of the BackgroundWorker’s host (which is usual a Form derived class) and this call doesn’t happen in the same thread as host was created (remember, never touch UI controls from non-creation threads). This situation yields unpredictable results, normally application just doesn’t respond anymore. The cause and the workarounds to this annoyance is described in this blog post. Basically you have three possibilities:



  1. No more calls into ToString()

  2. Disable func eval entirely (ouch)

  3. Avoid the problem

I choose to go with the point 1. and it works for me. Point 2 is the worst choice.
I hope that debugger folks will do something regarding this issue in the future as it makes you wonder what is going on until you find the description.

3 thoughts on “Problems debugging BackgroundWorker and solution

  1. If you make the ToString() method of the BackgroundWorker’s parent Form atomic, it seems this unpredicatibilty no longer occurs.

    I put something like this in forms:

    public override string ToString()
    {
    return “Disabled due to func-eval bug”;
    }

    Cheers,

Leave a Reply