Visual Studio 2015调试在多线程应用程序中不起作用 [英] Visual Studio 2015 Debug doesn't work in multithread application
问题描述
Application.Run(new Form1());
其中 Main()
是。 由于我对C#很新,我认为有一些问题我的方法是多线程,但显然它发生在我尝试 - 使用异步/等待与任务,使用 BackgroundWorker
组件,或简单的新的线程(myFunc ).Start()
。
只是要清楚。
- 代码本身工作得很好。
- 调试器本身也可以工作,在我的主线程中的断点上没有冻结和死锁。如果我从主线程启动代码 - 一切都很好。
- 我还在一个简单的
中为(int i = 0; i < Int32.MaxValue; ++ i)
功能 - 同样的问题。 - 还检查了不同版本的.NET:4.6,4.5,4.0。 VS2010(以前使用过的)也不会在VS2013(我只是想确保它是一个VS2015的问题)中出现这个问题。 ,我的朋友在同一个VS2015中也没有这个问题。
编辑:每个请求,测试表单的代码,我不断得到问题。只有三个按钮,标签和BackgroundWorker。总体方案与主要项目的代码相似。
public partial class Form1:Form
{
public Form1()
{
InitializeComponent();
}
const int period = 10000;
void FuncAsync(IProgress< int> progress)
{
for(int i = 0; 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 {
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 {
double part =(double)i / Int32.MaxValue;
int percent =(int)(part * 100.0);
if((i%period)== 0)
label1.Text = percent.ToString();
//是的,这将导致从不同线程
//访问UI的异常//如果我在异常窗口中按Break,我也将获得10秒冻结
}
private async void button1_Click(object sender,EventArgs e)
{
var progress = new Progress< int>(i = > label1.Text = i.ToString());
等待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托管过程)时,问题似乎消失了。
希望对他人有效。
In my project I have a heavy part of code that should be executed on a separate thread without blocking UI. When debugger hits the breakpoint inside this code, VS2015 freezes for 5-10 seconds. After that, if I try to continue debug (by pressing Step Over, Step In or Continue), the app goes from paused state to working state, Debugging Tools are ticking, but nothing happens and there's 0% of CPU utilization. If I press Break All then, the "cursor" (don't know the correct term) is shown at Application.Run( new Form1() );
in Program.cs where Main()
is.
As I'm pretty new to C#, I thought that there was some problem with my approach to multithreading, but apparently it happens whatever I try - using async/await with Tasks, using BackgroundWorker
component, or simple new Thread(myFunc).Start()
.
Just to be clear.
- The code itself works perfectly fine.
- Debugger itself also works, no freezes and "deadlocks" on breakpoints in my main thread. If I launch the code from main thread - everything is fine.
- I also checked it in a fully new solution on a simple
for ( int i = 0; i < Int32.MaxValue; ++i )
function - same problem. - Also checked on different versions of .NET: 4.6, 4.5, 4.0. Same everywhere.
- The problem doesn't appear neither in VS2010 (which I used before), nor in VS2013 (which I tried just to be sure that it's a VS2015 problem). However, my friend working with the same VS2015 also doesn't have this problem.
Edit: per request, code of test form where I keep getting the problem. Only three buttons, label, and BackgroundWorker. The overall scheme is similar to the code of the main project.
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();
}
}
I've experienced similar issues with VS2015 freezing (indefinitely) whilst debugging a multi thread WinForms application.
I had no problems debugging the same code in VS2013.
The problem appears to go away when I disable the VS hosting process (Project -> Properties -> Debug -> Enable the Visual Studio hosting process).
Hope that works for others.
这篇关于Visual Studio 2015调试在多线程应用程序中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!