Reputation: 295
I have a ASP.NET website where a user gets large data from a DB by clicking a button, but some times the user want to cancel the job(these jobs can take a very long time) and their session will hang while doing this job.
Is there any way to stop the execution of code and clean the already collected data ?
Is the best solution to have a table with all the jobIDs where a bit will determine if the code can continue and let the user change this from a button/link.
Upvotes: 0
Views: 3433
Reputation: 8359
You may consider using AJAX and abort the current Request if the user hits a cancel button.
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<script type="text/javascript" language="javascript">
var sm = Sys.WebForms.PageRequestManager.getInstance();
sm.add_initializeRequest(initRequest);
function initRequest(sender, args) {
if (sm.get_isInAsyncPostBack() &&
args.get_postBackElement().id == 'btnStart') {
args.set_cancel(true);
alert('Still progressing!\nPlease wait ...');
} else if (sm.get_isInAsyncPostBack() &&
args.get_postBackElement().id == 'btnCancel') {
sm.abortPostBack();
}
}
</script>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:UpdateProgress ID="UpdateProgress1" runat="server">
<ProgressTemplate>
<p>Processing....</p>
<asp:LinkButton ID="btnCancel" runat="server">Cancel</asp:LinkButton>
</ProgressTemplate>
</asp:UpdateProgress>
<asp:LinkButton ID="btnStart" runat="server" onclick="btnStart_Click">Start work!</asp:LinkButton>
</ContentTemplate>
</asp:UpdatePanel>
Just put the database query in the corresponding serverside eventHandler
public partial class asyncLongLasting : System.Web.UI.Page
{
protected void btnStart_Click(object sender, EventArgs e)
{
// your database query may start here
System.Threading.Thread.Sleep(5000);
}
}
Upvotes: 0
Reputation: 301
you can change your UI to allow users to submit their request plus cancel the existing request then save the user request in some table(will have a status column) you can write a windows service which will
1)read all the request (cancel or data) from users
2)for data request it will create a job and start the job and change the status of request (with in process)
3)for cancle it will stop the job and change the status with stoped
4)when the job will finished it will create the report in other table and change the status with complete
your UI will refresh automatically and for completed status the report icon will appear which will display report from table in new window.
Upvotes: 0