contextswitchdeadlock

岁酱吖の 提交于 2019-12-06 03:12:53
Hath

You can turn this off if you think you've definitely not got a deadlock situation:

Debug->Exceptions->Managed Debug Assistants menu in Visual Studio and uncheck the ContextSwitchDeadlock

This is an infinite loop. You need to let your application pump some messages at least once every 60 seconds to prevent this exception to happen. Try calling System.Threading.Thread.CurrentThread.Join(10) once in a while. There are other calls you can do that let the messages pump.

It seems that you are adding a new event handler each time you call InitialiseTimer. That way m_Timer_Elapsed will be called as many times as it has been added. You should add the event handler just one time.

Thiruvarul

If your application hangs or not reponse even after you uncheck the box against contextswitchdeadlock. Put the following line before call of method or for loop.

In C#

System.Windows.Forms.Application.DoEvents();

and VB.NET / VB / ASP.NET

DoEvents()

Couple thoughts/questions:

1) The code snippet looks like your interval is every 1 second (not 5 as mentioned in the comments). 2) The big question is what is RequestWork() doing?

Without knowing what RequestWork() is doing, we can't really comment on why you are seeing a ContextSwitchDeadlock.

Somethings to think about with respect to this method a) how long does it take? b) is it accessing GUI elements?

Some MSDN comments on Elapsed:

If you use the Timer with a user interface element, such as a form or control, assign the form or control that contains the Timer to the SynchronizingObject property, so that the event is marshaled to the user interface thread.

-and-

The Elapsed event is raised on a ThreadPool thread. If processing of the Elapsed event lasts longer than Interval, the event might be raised again on another ThreadPool thread. Thus, the event handler should be reentrant.

I'm thinking since you have a 1 second timer, you might want to look into what happens in RequestWork and see how long its taking.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!