3 c# debugging multithreading contextswitchdeadlock
在VS 2008中调试我的程序时,我遇到了以下错误:
CLR无法从COM上下文0x34fc1a0过渡到COM上下文0x34fc258 60秒.拥有目标上下文/公寓的线程很可能是在非抽空等待或处理非常长时间运行的操作而不抽取Windows消息.这种情况通常会对性能产生负面影响,甚至可能导致应用程序变得无响应或内存使用量随时间不断累积.为了避免这种情况
它似乎是死锁,即使代码只包含一个简单的C#计时器:请参阅下面的代码段:
private void RequestWork()
{
// The timer will be re-intialised if there are still no wating jobs in the database
StopTimer();
// assign all the threads some work
InitialiseTimer();
}
/// <summary>
/// Initialise a timer with a timer interval configured from app.config. Enable the timer and
/// register an appropriate event handler
/// </summary>
private void InitialiseTimer()
{
if (m_Timer == null)
{
// look up the default backoff time from the config
string backOffInt = ConfigurationSettings.AppSettings["BackOffInterval"];
int backoffInterval = 1000;
m_Timer = new System.Timers.Timer();
// set the timer interval to 5 seconds
m_Timer.Interval = backoffInterval;
m_Timer.Elapsed += new ElapsedEventHandler(m_Timer_Elapsed);
}
m_Timer.Enabled = true;
}
private void StopTimer()
{
if (m_Timer != null)
{
m_Timer.Enabled = false;
}
}
void m_Timer_Elapsed(object p_Sender, ElapsedEventArgs p_E)
{
RequestWork();
}
Run Code Online (Sandbox Code Playgroud)
据我所知,计时器应该运行,经过然后再次初始化,我看不出有死锁的本地原因.
我知道如何关闭这个错误消息,但觉得这不是一个解决方案,而是它掩盖了问题.
如果你认为你肯定没有陷入僵局,你可以关闭它:
Visual Studio中的Debug-> Exceptions-> Managed Debug Assistants菜单,取消选中ContextSwitchDeadlock
小智 5
这是一个无限循环。您需要让您的应用程序至少每 60 秒发送一次一些消息,以防止发生此异常。尝试偶尔调用 System.Threading.Thread.CurrentThread.Join(10) 一次。您还可以执行其他调用来传递消息。
| 归档时间: |
|
| 查看次数: |
5083 次 |
| 最近记录: |