Solved

interrupting method execution in C#

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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
C# Export DataGridView 4 41
Windows Service with UDP 2 28
Duplicate a row 2 33
c# code 19 61
Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

743 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

12 Experts available now in Live!

Get 1:1 Help Now