2

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.

3 Answers 3

2

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.

Sign up to request clarification or add additional context in comments.

4 Comments

Thx for your answer. public class CircuitBreaker : IInterceptor. where can i get IInterceptor?
IInterceptor comes with the Castle project. It's an IoC container. You can get that from castleproject.org If you don't want to use the castle project, then the second article has code that's standalone for a circuit breaker: go2.wordpress.com/…
thx. And which method is correct for my problem? in circuitbreaker2 or in IInterceptor?
Try the second one first. Less dependencies.
2

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.

Comments

0

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.