As the title says, I call a method which starts to load from a database. If a specific event is raised, I would like to prematurely exit that method without waiting for it to finish loading.
Edit: The method in question runs on the UI thread
exit() method exits current program by terminating running Java virtual machine. This method takes a status code. A non-zero value of status code is generally used to indicate abnormal termination. This is similar exit in C/C++.
Using return is the easiest way to exit a function. You can use return by itself or even return a value.
exit with status code 0 is normal and others are abnormal exits. Note that this is only a “good practice” and is not a strict rule that the compiler would care about. Also, it's worth noting when we invoke a Java program from the command-line that the status code is taken into account.
There is no way to (cleanly) destructively cancel a method from outside of that method.
Cancellation in the .NET Framework is typically a cooperative model. This means you're event would request a cancellation, and the method would, in turn, occasionally check for whether it should cancel and then exit cleanly.
This is handled via the CancellationToken
and CancellationTokenSource
types within the framework. For details, see the topic about Cancellation on MSDN.
If you execute your long-running method on UI thread than there is pretty much nothing built in that can help to stop it. Also UI thread will not be able to listen to any events (as it is blocked on the method execution).
Your options to make UI responsive and operation cancel-able:
To cancel - you may be able to cancel some operations by terminating/closing connection/file the operation is performed on. Note that most IO operations do not guarantee that such action will immediately terminate the method.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With