Dan*_*npe 5 .net c# debugging user-controls visual-studio-2010
嘿,我有一个UserControl不断崩溃我的Visual Studio.所以我运行了VS的另一个实例并调试了另一个VS,这就是我想的:
Collection was modified after the enumerator was instantiated.
Run Code Online (Sandbox Code Playgroud)
这是我的数组:
private static Color[] colors =
{
Color.FromArgb(155, 188, 255), // 40000
Color.FromArgb(156, 189, 255), // 39500
Color.FromArgb(157, 188, 255), // 39000
Color.FromArgb(156, 189, 254), // 38500
};
Run Code Online (Sandbox Code Playgroud)
这是我的循环崩溃的商务
public Heater()
{
InitializeComponent();
this.tarTemp = this.curTemp;
new Thread(() => UpdateTemp(true)).Start();
}
private delegate void UpdateTempDelegate(bool loop);
private void UpdateTemp(bool loop)
{
if (lblTemp.InvokeRequired)
{
UpdateTempDelegate del = new UpdateTempDelegate(UpdateTemp);
lblTemp.Invoke(del, loop);
}
else
{
do
{
lblTemp.Text = curTemp + C;
if (curTemp >= 0)
{
int i = curTemp - 10;
if (i < 0)
i = 0;
if (i > colors.Length - 1)
i = colors.Length - 1;
this.BackColor = colors[i]; // I'M CRASHING !!!
}
} while (loop && !this.Disposing);
}
}
Run Code Online (Sandbox Code Playgroud)
崩溃Visual Studio Designer的行是 this.BackColor = colors[i];
这是运行线程的图像:

所有线程停在同一行...... this.BackColor = colors[i];
这是EventViewer崩溃日志:
Application: devenv.exe
Framework Version: v4.0.30319
Description: The process was terminated due to an unhandled exception.
Exception Info: System.InvalidOperationException
Stack:
at System.ThrowHelper.ThrowInvalidOperationException(System.ExceptionResource)
at System.Collections.Generic.SortedList`2+SortedListValueEnumerator[[System.Int32, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089],[System.__Canon, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]].MoveNext()
at Microsoft.VisualStudio.Shell.ServiceProviderHierarchy.GetService(System.Type)
at System.ComponentModel.Design.ServiceContainer.GetService(System.Type)
at System.ComponentModel.Design.DesignerHost.GetService(System.Type)
at System.ComponentModel.Design.DesignerHost+Site.System.IServiceProvider.GetService(System.Type)
at System.Windows.Forms.Control.get_AmbientPropertiesService()
at System.Windows.Forms.Control.get_BackColor()
at System.Windows.Forms.Control.set_BackColor(System.Drawing.Color)
at Multiplier.Heater.UpdateTemp(Boolean)
at Multiplier.Heater.<.ctor>b__0()
at System.Threading.ThreadHelper.ThreadStart_Context(System.Object)
at System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object, Boolean)
at System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object)
at System.Threading.ThreadHelper.ThreadStart()
Run Code Online (Sandbox Code Playgroud)
这是我到目前为止遇到的最奇怪的事情.帮助应该是适当的.
正如您所发现的,您的代码正在使设计器崩溃,从而使 VS 崩溃。问题是您在设计模式下启动一个线程,由设计器在设计时运行一些代码触发。例如,它将运行构造函数、Load 事件、OnHandleCreated 等。这带来了非常好的设计时体验,您的控件看起来就像在运行时一样。
但这也会导致很多问题。您必须避免运行在不同的执行上下文中运行时可能导致异常的代码。经典示例是尝试在不指定完整路径的情况下打开文件、打开与数据库服务器脱机或无法访问的数据库连接。并且肯定会启动一个线程,当设计者构造和销毁本机窗口句柄时,InvokeRequired 不会可靠地工作。修复方法很简单:
public Heater()
{
InitializeComponent();
this.tarTemp = this.curTemp;
if (!this.DesignMode) {
new Thread(() => UpdateTemp(true)).Start();
}
}
Run Code Online (Sandbox Code Playgroud)
您需要做更多的工作,此代码在运行时也无法正常工作。当放置用户控件的窗体关闭时,线程代码将会崩溃。一旦解决了这个问题,它现在在设计时也能正常工作的可能性很大。但不要。
| 归档时间: |
|
| 查看次数: |
1564 次 |
| 最近记录: |