Solved

interrupting method execution in C#

Posted on 2011-09-20
3
345 Views
Last Modified: 2012-05-12
Hi Experts,
is there any way to iinterrupt the execution of a method in c# without using threads?
0
Comment
Question by:dionisio_79
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 1

Accepted Solution

by:
RG59 earned 250 total points
ID: 36566294
I'm not sure what you're trying to achieve. If you mean cancel all further execution of a method once a particular set of conditions are met then you could test for the condition at strategicpoints in your method...

if (conditionsMet()) return;

For a long running method that's processing a list or a collection etc then you put this inside your processing loop and you might want to use 'break' instead of 'return' to allow for finalisation processing outside the loop.

If you mean pause a method so it can pick up where it left off later then you could use a similar approach but you'll need to save the state and include logic that allows the method to pick up half way through. I guess you might pass the state in as an object that you can update. That way the calling code can take care of saving it away and making sure it passes the correct state back in.

If you want the user to have a cancel button then you're into more than one thread anyway, otherwise your UI is locked.

Hope this helps. Please post more details if you meed a more precise answer.
0
 
LVL 8

Assisted Solution

by:Volox
Volox earned 250 total points
ID: 36574111
I agree with RG59... as soon as you start talking about outside intervention causing the pause, you are by nature talking about threads.  The "without using threads" is kind of a misnomer anyway since any time you run a program you are using at least one thread, but I assume you meant you didn't want to use thread control code.  In which case, I 'd ask the question, why the avoidance?

The only real difference between what RG59 is suggesting with the boolean checks at a strategic point and what you would probably do with threading is that threading your 'flags' would be locks or wait events (such as ManualResetEvent).

Regardless of which way you implement (boolean flags or wait events), you still need to be thinking about threading because if you implement the boolean flag as a static variable or member variable, then you have to realize that if multiple threads are going through the method they will all get cancelled when you flip the flag.  There are certainly techniques that can be used so that you can cancel on a per-method-running-instance basis, but whether that is nessecary depends on your intent.

More details aredefinitely needed to give you a more complete answer.  Maybe some psuedo-code or a use case that describes what you are trying to do?  And an explanation of why you want to avoid thread control code?
0
 

Author Closing Comment

by:dionisio_79
ID: 36587119
tHANKS for replies I just used the return method to exit.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question