Saturday, January 06, 2007

C#写的多线程程序出现下面的错误是怎么回事?

发信人: ahxnwubh1983 (小碧), 信区: DotNET
标 题: C#写的多线程程序出现下面的错误是怎么回事?
发信站: 水木社区 (Fri Jan 5 22:14:02 2007), 站内

ContextSwitchDeadlock was detected
Message: The CLR has been unable to transition from COM context 0x1a0968 to COM context 0x1a0ad8 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.

请大侠帮忙,谢谢了!

--

※ 来源:·水木社区 newsmth.net·[FROM: 211.99.222.*]

[本篇全文] [本篇作者:pseudocode] [进入讨论区] [返回顶部]2发信人: pseudocode (I can Run!), 信区: DotNET
标 题: Re: C#写的多线程程序出现下面的错误是怎么回事?
发信站: 水木社区 (Fri Jan 5 23:15:40 2007), 站内

你的程序有调用Com的东西或者unmanaged的吗?

【 在 ahxnwubh1983 (小碧) 的大作中提到: 】
: ContextSwitchDeadlock was detected
: Message: The CLR has been unable to transition from COM context 0x1a0968 to COM context 0x1a0ad8 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 o
: 请大侠帮忙,谢谢了!
: ...................

--

※ 来源:·水木社区 newsmth.net·[FROM: 222.129.40.*]

[本篇全文] [本篇作者:ahxnwubh1983] [进入讨论区] [返回顶部]3发信人: ahxnwubh1983 (小碧), 信区: DotNET
标 题: Re: C#写的多线程程序出现下面的错误是怎么回事?
发信站: 水木社区 (Sat Jan 6 03:44:09 2007), 站内

呵呵,我初学c#,都是用c#语言提供的基本功能,只是创建了多个线程
好像看意思的线程互斥死锁了
【 在 pseudocode (I can Run!) 的大作中提到: 】
: 你的程序有调用Com的东西或者unmanaged的吗?


--

※ 来源:·水木社区 newsmth.net·[FROM: 211.99.222.*]

[本篇全文] [本篇作者:pseudocode] [进入讨论区] [返回顶部]4发信人: pseudocode (I can Run!), 信区: DotNET
标 题: Re: C#写的多线程程序出现下面的错误是怎么回事?
发信站: 水木社区 (Sat Jan 6 08:51:27 2007), 站内

你的某个线程是不是有无限循环或者需要操作时间很长的啊?

【 在 ahxnwubh1983 (小碧) 的大作中提到: 】
: 呵呵,我初学c#,都是用c#语言提供的基本功能,只是创建了多个线程
: 好像看意思的线程互斥死锁了


--

※ 来源:·水木社区 newsmth.net·[FROM: 222.129.33.*]

[本篇全文] [本篇作者:pseudocode] [进入讨论区] [返回顶部]5发信人: pseudocode (I can Run!), 信区: DotNET
标 题: Re: C#写的多线程程序出现下面的错误是怎么回事?
发信站: 水木社区 (Sat Jan 6 09:02:29 2007), 站内

Why you sometimes get a bogus ContextSwitchDeadLock MDA under the debugger
The ContextSwitchDeadLock MDA (I blogged about MDAs here) sometimes fires under the debugger. MSDN says:

It is possible for this MDA to be falsely activated when all of the following conditions are met:
* An application creates COM components from STA threads either directly or indirectly through libraries.
* The application was stopped in the debugger and the user either continued the application or performed a step operation.
*Unmanaged debugging is not enabled.

The reasoning is that:
1) When you're stopped in the debugger while managed-only debugging, unmanaged threads are still running. This means that any unmanaged threads that are waiting on some timeout from managed code will continue to run. The unmanaged thread will see the timeout fire, but it won't realize that the managed thread is actually stopped by the debugger. Thus the managed thread looks it's deadlocked. This is not an issue when unmanaged debugging because then the timeout thread is also frozen when stopped in the debugger, and so the timeout won't fire.

2) The finalizer for an STA COM objects needs to run code on the STA thread. So there's some cross-thread stuff between the finalizer thread and the STA thread.

So the STA thread may be blocked by the debugger (since the whole managed process is frozen at a breakpoint), while the timeout check (on an unmanaged thread) is still ticking.

This is a race because it needs the finalization and debugger event to happen at just the right windows.
We assessed that this scenario as a rare situation. I’d expect you to see this only on very rare occasions (due to prerequisite timing issues).
If you are hitting this bogusly, one workaround is to disable this specific MDA.

http://blogs.msdn.com/jmstall/archive/2005/11/11/ContextSwitchDeadLock.aspx
http://eknowledger.spaces.live.com/blog/cns!F475D4DE444DB1AB!1244.entry
【 在 ahxnwubh1983 (小碧) 的大作中提到: 】
: 呵呵,我初学c#,都是用c#语言提供的基本功能,只是创建了多个线程
: 好像看意思的线程互斥死锁了


--

※ 来源:·水木社区 newsmth.net·[FROM: 222.129.33.*]

[本篇全文] [本篇作者:ahxnwubh1983] [进入讨论区] [返回顶部]6发信人: ahxnwubh1983 (小碧), 信区: DotNET
标 题: Re: C#写的多线程程序出现下面的错误是怎么回事?
发信站: 水木社区 (Sat Jan 6 13:45:33 2007), 站内

线程函数中会循环Sleep50ms,然后去检查一个消息队列是否有消息进行处理
是不是因为Sleep的次数太多?谢谢了
【 在 pseudocode (I can Run!) 的大作中提到: 】
: Why you sometimes get a bogus ContextSwitchDeadLock MDA under the debugger
: The ContextSwitchDeadLock MDA (I blogged about MDAs here) sometimes fires under the debugger. MSDN says:
: It is possible for this MDA to be falsely activated when all of the following conditions are met:
: ...................

--

※ 修改:·ahxnwubh1983 于 Jan 6 13:45:45 修改本文·[FROM: 211.99.222.*]
※ 来源:·水木社区 newsmth.net·[FROM: 211.99.222.*]

[本篇全文] [本篇作者:ahxnwubh1983] [进入讨论区] [返回顶部]7发信人: ahxnwubh1983 (小碧), 信区: DotNET
标 题: Re: C#写的多线程程序出现下面的错误是怎么回事?
发信站: 水木社区 (Sat Jan 6 14:32:09 2007), 站内

发生这个错误的时候,所有线程都是停止在sleep调用上
【 在 ahxnwubh1983 (小碧) 的大作中提到: 】
: 线程函数中会循环Sleep50ms,然后去检查一个消息队列是否有消息进行处理
: 是不是因为Sleep的次数太多?谢谢了


--

※ 来源:·水木社区 newsmth.net·[FROM: 211.99.222.*]

[本篇全文] [本篇作者:Nineteen] [进入讨论区] [返回顶部]8发信人: Nineteen (在哪里倒下,就在哪里躺下!), 信区: DotNET
标 题: Re: C#写的多线程程序出现下面的错误是怎么回事?
发信站: 水木社区 (Sat Jan 6 14:34:07 2007), 站内

当队列中有新的Item加进来,应该让队列对象主动的push一个event,而不应该让线程去拉.

【 在 ahxnwubh1983 (小碧) 的大作中提到: 】
: 线程函数中会循环Sleep50ms,然后去检查一个消息队列是否有消息进行处理
: 是不是因为Sleep的次数太多?谢谢了


--
数年前的这一天,她正被那携带着巨大骨龙的灰袍女孩拦下。银眸、黑发、
赤足以及巨大的死神镰刀,一起构成了一副令人永生不忘的画卷。

那一刻的感觉,自此铭刻在她的心底,并在此时重新升起,占据了她意识的
全部空间,从此定格。


※ 来源:·水木社区 newsmth.net·[FROM: 221.217.175.*]

[本篇全文] [本篇作者:Nineteen] [进入讨论区] [返回顶部]9发信人: Nineteen (在哪里倒下,就在哪里躺下!), 信区: DotNET
标 题: Re: C#写的多线程程序出现下面的错误是怎么回事?
发信站: 水木社区 (Sat Jan 6 14:34:58 2007), 站内

或者让线程等在一个event上也好啊
【 在 Nineteen (在哪里倒下,就在哪里躺下!) 的大作中提到: 】
: 当队列中有新的Item加进来,应该让队列对象主动的push一个event,而不应该让线程去拉.


--
数年前的这一天,她正被那携带着巨大骨龙的灰袍女孩拦下。银眸、黑发、
赤足以及巨大的死神镰刀,一起构成了一副令人永生不忘的画卷。

那一刻的感觉,自此铭刻在她的心底,并在此时重新升起,占据了她意识的
全部空间,从此定格。


※ 来源:·水木社区 newsmth.net·[FROM: 221.217.175.*]

[本篇全文] [本篇作者:ahxnwubh1983] [进入讨论区] [返回顶部]10发信人: ahxnwubh1983 (小碧), 信区: DotNET
标 题: Re: C#写的多线程程序出现下面的错误是怎么回事?
发信站: 水木社区 (Sat Jan 6 14:35:40 2007), 站内

这二者有什么区别吗?
【 在 Nineteen (在哪里倒下,就在哪里躺下!) 的大作中提到: 】
: 当队列中有新的Item加进来,应该让队列对象主动的push一个event,而不应该让线程去拉.


--

※ 来源:·水木社区 newsmth.net·[FROM: 211.99.222.*]

[本篇全文] [本篇作者:pseudocode] [进入讨论区] [返回顶部]11发信人: pseudocode (I can Run!), 信区: DotNET
标 题: Re: C#写的多线程程序出现下面的错误是怎么回事?
发信站: 水木社区 (Sat Jan 6 14:44:38 2007), 站内

一个是主动去看有没有要处理的
另一个是有东西要处理了才醒过来

【 在 ahxnwubh1983 (小碧) 的大作中提到: 】
: 这二者有什么区别吗?


--

※ 来源:·水木社区 newsmth.net·[FROM: 222.129.33.*]

No comments: