Search code examples
c#visual-studio-2015backgroundworker

VS 2015 Cannot step through backgroundworker_dowork


I'm using C# in VS 2015 targeting the 4.5.2 framework for a normal Windows application (nothing special, actually). I recently moved the project from VS 2013, in case that could be a factor.

Now, when I press F5 I can step (line-by-line) through the Form_Load event. Then a timer ticks and I can step through that code as well. The timer stops itself and calls bgw1.RunWorkerAsync(). I can set a break point inside the bgw1.DoWork routine and it does stop but as soon as it stops, I get a swirling disk and I must wait for several seconds before I regain control over VS. Then, for a moment, every looks normal. I press F10 to step one line and then everything stops functioning as one would expect (VS looks like it is running the code but that doesn't seem to be happening).

This is super-easy to reproduce (on my system):

  1. Open VS 2015
  2. New project...
  3. Visual C# / Windows / Classic Desktop
  4. Add a timer named "tmr1"; enabled; interval=1000
  5. Add a backgroundworker
  6. Change the Form1 code to what you see below
  7. Set breakpoints on every line that includes "a = "

I can step through Form_Load, Tmr1_Tick, but not bgw1_DoWork.

using System;
using System.ComponentModel;
using System.Windows.Forms;

namespace BGWTest
{
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        int a, b, c;
        a = 5; // <-- breakpoint here (can step through this routine)
        b = 7;
        c = a * b;
        this.Text = c.ToString();
        tmr1.Start();
    }

    private void tmr1_Tick(object sender, EventArgs e)
    {
        tmr1.Stop();

        int a, b, c;
        a = 15;  // <-- breakpoint here (can step through this routine)
        b = 17;
        c = a * b;
        this.Text = c.ToString();
        bgw1.RunWorkerAsync();
    }

    private void bgw1_DoWork(object sender, DoWorkEventArgs e)
    {
        int a, b, c;
        a = 25;  // <-- breakpoint here (here is where the problem begins)
        b = 27;
        c = a * b;
        UpdateText(c);
    }

    private delegate void UpdateTextCallback(int value);
    private void UpdateText(int value) {
        if (InvokeRequired) {
            UpdateTextCallback deleg = new UpdateTextCallback(UpdateText);
            this.Invoke(deleg, new object[] { value });
        } else {
            this.Text = value.ToString();
        }
    }
}
}

I break the code (after trying to take one step inside the bgw_dowork) but it's on the Application.Run() line which launched the form. The form behaves as if it is not running anything (nothing being updated in the UI).

By the way, as long as I don't break in the bgw_dowork routine, everything works fine.

This is something I've not seen before and I've not found anything helpful on SO or anywhere else. I found someone said delete the .suo file and all will be fine. I did this, didn't work.

Then I realized that I had just installed NUnit (through nuget) so I removed it but the problem remains.

Then I deleted all my temp files, did not help. Then I repaired my VS 2015 install but the problem remains. Then I uninstalled and re-installed VS 2015, but the problem is the same. The same code works fine in VS 2013...so what could be different with VS 2015?

I might be missing something quite obvious and I would appreciate any insights someone can offer.


Solution

  • It was a bug in VS 2015 which has been fixed in Update 1.

    After updating to VS2015.1 it works fine (can step through the backgroundworker_dowork).

    According to the Help Center, this is on-topic but since it's a fixed bug perhaps this whole question should be deleted. I will leave that in the hands of the community.