C# ASP.NET:停止代码执行
我有一个ASP.NET网站,用户通过单击按钮从数据库获取大量数据,但有时用户希望取消作业(这些作业可能需要很长时间),并且在执行此作业时,他们的会话将挂起 有没有办法停止代码的执行并清理已收集的数据C# ASP.NET:停止代码执行,c#,asp.net,C#,Asp.net,我有一个ASP.NET网站,用户通过单击按钮从数据库获取大量数据,但有时用户希望取消作业(这些作业可能需要很长时间),并且在执行此作业时,他们的会话将挂起 有没有办法停止代码的执行并清理已收集的数据 是一个最好的解决方案,让所有的Jobe都有一个表来确定代码是否可以继续,并让用户从按钮/链接中改变它。 < P>你可以考虑使用Ajax,如果用户点击了取消按钮,就中止当前请求。p> <asp:ScriptManager ID="ScriptManager1" runat="server
是一个最好的解决方案,让所有的Jobe都有一个表来确定代码是否可以继续,并让用户从按钮/链接中改变它。
< P>你可以考虑使用Ajax,如果用户点击了取消按钮,就中止当前请求。p> <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>
您可以更改UI以允许用户提交其请求并取消现有请求,然后将用户请求保存在某个表中(将有一个状态列) 您可以编写一个windows服务,它将
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
您的UI将自动刷新,对于已完成状态,将显示报告图标,该图标将在新窗口中显示来自表的报告。重构您的设计以节省时间这是非常大的数据,因此需要一些时间您使用的.Net Framework版本是什么?如果您是在.Net 4.0中编程,那么您可以通过Tasks将耗时的任务推送到单独的线程上,然后使用TPL的task cancellation功能。用户只需取消自己正在进行的任务,而不仅仅是检索前10行,然后添加分页
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