在 VS 2008 中调试我的程序时遇到以下错误:
CLR 在 60 秒内无法从 COM 上下文 0x34fc1a0 转换到 COM 上下文 0x34fc258。拥有目标上下文/单元的线程很可能正在执行非泵等待或处理一个非常长时间运行的操作而不发送 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();
}
据我所知,计时器应该运行、计时结束然后再次初始化,我看不出本地死锁的原因。
我知道如何关闭此错误消息,但感觉这不是解决方案,而是掩盖了问题。
最佳答案
这是一个无限循环。您需要让您的应用程序至少每 60 秒发送一次消息,以防止发生此异常。 尝试偶尔调用 System.Threading.Thread.CurrentThread.Join(10)。您还可以执行其他调用来发送消息。
关于c# - 上下文切换死锁,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/334360/