I have two buttons: one is "save" and the other is "stop" in asp.net
web form. If I click on the save button the page is going to load and the button is going to do the operation. The operation takes five minutes. If I click on the stop button I do not want to perform any operations and I need to stop the loading .
-
There are a lot of ways to accomplish this depending on the situation, and without any sample code, we wouldn't be able to possibly answer your question. – rikitikitik Aug 28 '14 at 02:47
2 Answers
In general, there is no way to do this on the server side. One request from the browser is unrelated to the next. Even if the first request is to a given page, and the Stop request is a request to the same page, there will be two separate instances of the page class, each going through its own life cycle.
Now, you may be able to do something very fancy with cancellable async operations if you're running .NET 4.0 (preferably 4.5). In this case, your long-running operation would have to be listening to a cancellation token, and would have to stop itself if the token were signaled. You would have to store the token in Session state, or have some static
array of cancellation tokens, and put the index into Session state.
More generally, your long-running operation has to listen for some signal that says it's time to give up and stop. The "Stop" request would have to set that signal.

- 160,644
- 26
- 247
- 397
Ideally, your long running task should be put on a separate thread that is launched by the page when the 'Start' button is clicked. That way, your page posts back to the browser immediately and users won't see the hourglass.
You can store the status of the thread in a session object and also display on the page the status of the process on the page (which, as I said, posts back immediately after starting the process).
When clicking 'Stop', the page checks if the process is running and stops the thread by either directly killing the thread or having the thread check for some "Stop" flag periodically.
Here is help on threads: right way to create thread in ASP.NET web application
-
-
Well, the OP has not mentioned that this process is unique to each user. If it is unique, then he may use the session object instead. – navigator Aug 28 '14 at 10:21
-
-
Not really - it depends on the application. We have several apps in production where some long running processes are handled by timers or can be started by administrators. If an administrator starts a task, another administrator cannot start it until it ends... – navigator Aug 29 '14 at 04:00
-
I said _assumed_. An administrative situation is, by definition, an exception, and not a rule. And, BTW, this isn't the sort of task that should be run in ASP.NET. In general, ASP.NET can start (and stop) such tasks, but they should run in a more robust environment like a Windows service, perhaps with a reliable queue to process requests. – John Saunders Aug 29 '14 at 04:34