c# - Visual Studio 2015 调试在多线程应用程序中不起作用

标签 c# multithreading debugging visual-studio-2015 visual-studio-debugging

在我的项目中,我有大量代码应该在单独的线程上执行而不会阻塞 UI。当调试器遇到此代码中的断点时,VS2015 会卡住 5-10 秒。之后,如果我尝试继续调试(通过按 Step Over、Step In 或 Continue),应用程序会从暂停状态变为工作状态,调试工具正在滴答作响,但什么也没有发生,CPU 利用率为 0%。如果我然后按 Break All,“光标”(不知道正确的术语)显示在 Application.Run( new Form1() ); 处。在 Program.cs 中 Main()是。

由于我是 C# 的新手,我认为我的多线程方法存在一些问题,但显然无论我尝试什么都会发生 - 使用 async/await with Tasks,使用 BackgroundWorker组件,或简单的 new Thread(myFunc).Start() .

只是为了清楚。

  • 代码本身运行良好。
  • 调试器本身也可以工作,在我的主线程中的断点上没有卡住和“死锁”。如果我从主线程启动代码 - 一切都很好。
  • 我还在一个简单的 for ( int i = 0; i < Int32.MaxValue; ++i ) 上用一个全新的解决方案检查了它功能 - 同样的问题。
  • 还检查了不同版本的 .NET:4.6、4.5、4.0。到处都一样。
  • 这个问题既没有出现在 VS2010(我以前使用过)中,也没有出现在 VS2013 中(我试过它只是为了确保这是一个 VS2015 问题)。 不过,我的 friend 用同样的VS2015也没有这个问题。

编辑:根据请求,我不断遇到问题的测试表单代码。只有三个按钮,label 和 BackgroundWorker。整体方案与主工程代码类似。

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

    const int period = 10000;
    void FuncAsync(IProgress<int> progress)
    {
        for ( int i = 0; i < Int32.MaxValue; ++i )
        {
            double part = (double)i / Int32.MaxValue;
            int percent = (int)(part * 100.0);

            if ( (i % period) == 0 )
                progress.Report( percent );
        }
    }
    void FuncBW(BackgroundWorker worker)
    {
        for ( int i = 0; i < Int32.MaxValue; ++i )
        {
            double part = (double)i / Int32.MaxValue;
            int percent = (int)(part * 100.0);

            if ( (i % period) == 0 )
                worker.ReportProgress( percent );
        }
    }
    void FuncThread()
    {
        for ( int i = 0; i < Int32.MaxValue; ++i )
        {
            double part = (double)i / Int32.MaxValue;
            int percent = (int)(part * 100.0);

            if ( (i % period) == 0 )
                label1.Text = percent.ToString();
            //yes, this one will cause exception of accessing UI from different thread
            //if i press "Break" in the exception window, i will also get a 10sec freeze
        }
    }



    private async void button1_Click(object sender, EventArgs e)
    {
        var progress = new Progress<int>(i => label1.Text = i.ToString() );
        await Task.Factory.StartNew( () => FuncAsync( progress ),
                                    TaskCreationOptions.LongRunning );
    }

    private void button2_Click(object sender, EventArgs e)
    {
        backgroundWorker1.RunWorkerAsync();
    }

    private void button3_Click(object sender, EventArgs e)
    {
        Thread t = new Thread(FuncThread);
        t.Start();
    }

    private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
    {
        FuncBW( (BackgroundWorker)sender );
    }

    private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
    {
        label1.Text = e.ProgressPercentage.ToString();
    }
}

最佳答案

我在调试多线程 WinForms 应用程序时遇到过类似的 VS2015 卡住(无限期)问题。

我在 VS2013 中调试相同的代码没有问题。

当我禁用 VS 托管进程(项目 -> 属性 -> 调试 -> 启用 Visual Studio 托管进程)时,问题似乎消失了。

希望对其他人有用。

关于c# - Visual Studio 2015 调试在多线程应用程序中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32051488/

相关文章:

c - 与没有互斥锁的工作线程交互

c# - ThreadPool RegisterWaitForSingleObject 的资源使用

java - 复制 volatile ArrayList 的元素

vb6 - 如何强制 VB6 在没有断点的情况下从程序执行进入调试器?

c# - 如何在 Zedgraph 中设置 Y 轴的范围?

c# - 'CustomerId' 操作的结果中不存在所需的列 'FromSql'

c# - 如何在 IIS 上运行电报机器人

c# - 此应用程序需要以下版本之一的 .NET Framework

ios - 代码中的错误导致按钮并不总是执行操作

php:不知从何而来的空行