C# QueueUserWorkItem不允许返回响应
我正在编写一个ASP.NET3.5Web应用程序,我想从web“启动并忘记”一项任务 想法如下:C# QueueUserWorkItem不允许返回响应,c#,jquery,asp.net,ajax,asynchronous,C#,Jquery,Asp.net,Ajax,Asynchronous,我正在编写一个ASP.NET3.5Web应用程序,我想从web“启动并忘记”一项任务 想法如下: 用户进入带有表格的网页,选择一些项目,并通过按钮提交数据 按下该按钮后,将用数据发回AJAX请求 ASP.NET应用程序启动(后台?)进程。如果该过程成功启动,应用程序将通过AJAX回复客户端 该进程使用数据创建一个文本文件,将其保存到一个目录,并启动一个SSIS包(该包在进程完成后向用户发送邮件),因此无需回调 我的问题是,当我使用ThreadPool.QueueUserWorkItem()启
- 用户进入带有表格的网页,选择一些项目,并通过按钮提交数据
- 按下该按钮后,将用数据发回AJAX请求
- ASP.NET应用程序启动(后台?)进程。如果该过程成功启动,应用程序将通过AJAX回复客户端
- 该进程使用数据创建一个文本文件,将其保存到一个目录,并启动一个SSIS包(该包在进程完成后向用户发送邮件),因此无需回调
$("#btn-actualizar-instrumentos").click(function(e) {
e.preventDefault();
var contador = 0;
$.each(state, function(i, val) {
contador += val.length;
});
if (contador == 0) {
alert("No se han seleccionado instrumentos para actualizar.");
return;
}
else {
$("#btn-actualizar-instrumentos").button("option", "disabled", true)
$.blockUI({ message: "Enviando instrumentos seleccionados..." });
var textoFinal = JSON.stringify(state);
var idsFinal = JSON.stringify(ids);
$.ajax({
type: "POST",
url: 'SDI_obtenerarchivosbloomberg.aspx',
//data: textoFinal,
data: idsFinal,
success: function(response) {
$("#div-resultados").html(response.mensaje);
$("#div-resultados").dialog({
draggable: false
});
if (!response.oK) {
$("#btn-actualizar-instrumentos").button("option", "disabled", false);
}
},
error: function(xhr, msg, msg2) {
$("#div-resultados").html(xhr);
$("#btn-actualizar-instrumentos").button("option", "disabled", false);
},
complete: function(e) {
setTimeout(function() {
$.unblockUI();
}, 0);
}
});
}
});
ASP.NET代码:
public partial class PageClass1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
var instrumentos = new List<decimal>();
// File generation and other tasks...
// Invocation of fire-and-forget task
var taskQueuing = ThreadPool.QueueUserWorkItem(c => DoWork(instrumentos));
// Building response
mensaje += "<p>\n";
if (taskQueuing)
{
mensaje += "Instrumentos procesados.<br />\n";
mensaje += "Se le notificará por correo electrónico cuando la operación de actualización esté completa.<br />\n";
mensaje += "Puede cerrar esta ventana ahora.<br />\n";
}
else
{
mensaje += "Error al enviar instrumentos:\n";
}
mensaje += "</p>\n";
// JSONing response
Response.StatusCode = 200;
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.ContentType = "application/json";
Response.Write(textoRespuesta);
Response.Close();
}
private void DoWork(List<decimal> lista)
{
// Invoking SSIS package
}
}
public分部类PageClass1:System.Web.UI.Page
{
受保护的无效页面加载(对象发送方、事件参数e)
{
var instrumentos=新列表();
//文件生成和其他任务。。。
//调用火与遗忘任务
var taskruing=ThreadPool.QueueUserWorkItem(c=>DoWork(instrumentos));
//建筑物响应
mensaje+=“\n”;
if(任务队列)
{
mensaje+=“仪表程序”
\n;
mensaje+=“在完成任务后的操作过程中使用电气设备的通知”
\n;
mensaje+=“Puede cerrar esta ventana ahora.
\n”;
}
其他的
{
mensaje+=“环境仪表错误:\n”;
}
mensaje+=“\n”;
//JSN响应
Response.StatusCode=200;
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.ContentType=“application/json”;
响应。写入(textoRespuesta);
Response.Close();
}
专用作废工作(列表A)
{
//调用SSIS包
}
}
我怎样才能解决这个问题
提前感谢。作为一个旁白,它不是有点重量级了吗?一个ASPX页面,也许一个WebAPI端点可能更有益/更适合这个目的,无论如何,我感觉你必须以某种方式将页面标记为异步的,因为我不确定在页面生命周期结束时如何处理页面的范围。请查看此链接:也许吧。谢谢您的提示。有了它和一些Fiddler,我发现响应正确地返回,因此JQuery(客户端)肯定有问题。