Debug Error: ContextSwitchDeadlock was detected

利用Visual Studio 2005开发工具时,如果我们有很大量的数据进行处理,譬如3万多条数据写入Excel,对数据库进行频繁的读出写入,有可能会出现一下错误信息:

 

"ContextSwitchDeadlock was detected" error message popping up when debugging.

 

The CLR has been unable to transition from COM context 0x197060 to COM context 0x196ef0 for 60 seconds. The thread that owns the destination context/apartment is most likely either doing a non-pumping wait or processing a very long running operation without pumping Windows messages. This situation generally has a negative performance impact and may even lead to the application becoming non responsive or memory usage accumulating continually over time. To avoid this problem, all single threaded apartment (STA) threads should use pumping wait primitives (such as CoWaitForMultipleHandles) and routinely pump messages during long running operations.

 

通常我们的解决方法是:在Debug -> Exceptions -> Managed Debug Assistants 去掉ContextSwitchDeadlock一项前面的钩。

 

如果代码中没有对com对象的操作,那么这是一个非常简单而且有效的方法。

 

然而,如果我们的代码真的有对com对象的操作,例如写入Excel,是不是真的会有一个deadlockexception,并且会在以后导致代码crash呢?在这种情况下,我们可以设置一个计数器,比如每写入100条数据,就进行一下Application.DoEvents()操作,来通知主线程,不是我们的代码中真的有deadlock发生,而是某一个操作太长了。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值