Solved

interrupting method execution in C#

Posted on 2011-09-20
3
342 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
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…

920 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now