开发者

code executed too long, how to stop executing

We have C# application that crawling and executing code.开发者_开发知识库 But some time system stolp responding becouse code executing too long. How can we stop executing code after 10s that application will not stop responding any more.


The approaches taken to tackle this problem are dependent on the way you've designed your long running operation. So, without further details, I can only provide general pointers.

If your code takes to long to execute because you're not getting a response from a remote system (ie. db, website, etc) in time, then consider timeouts. If the API you use for making those remote calls, doesn't support timeouts, consider something like the CircuitBreaker pattern:

http://davybrion.com/blog/2009/07/protecting-your-application-from-remote-problems/ http://timross.wordpress.com/2008/02/10/implementing-the-circuit-breaker-pattern-in-c/

If it's simply that your application is doing a lot of work, make sure you do that work on a thread other than the UI thread, as Twitch said, to keep the UI responsive.

If you're using a very long loop doing internal work, then it could be worth checking repeatedly in that loop for a cancelation condition being met (this could be a flag set from a different thread or even elapsed time). This approach is called cooperative cancellation. This article on the .Net 4.0 cancellation framework gives some good background, along with this article which it references.


You have to add some sort of way for the program to tell windows "no, it's not frozen, it's working", either by making all the processing and crawling in another thread, or by doing some form of notice, like printing something every few frames.


You can call Application.DoEvents to perform events while performing a long task in a GUI thread. That way it won't block the GUI. You should consider running the long task in a thread itself though, since you get a lot more direct control then.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜