线程在长时间的 SP 执行中被中止?
我创建了一个执行很长 sp 的 asp.net 页面(SSMS 中大约 1 小时的 sp 执行时间)。
有一个 Admin.aspx 可以激活此 SP。
在 web.config 中我添加了:
<httpRuntime maxRequestLength="111264" requestValidationMode="2.0" executionTimeout="10000000" />
还在连接属性中添加了:
_cmd.CommandTimeout = 0; // unlimited
该函数正在使用 jquery ajax (到 ashx)来激活 sp (它工作正常)
jquery 调用一个 ashx 文件来激活 sp 。
但是,我不希望 UI 将被阻止直到操作结束,因此我在 ashx 文件中创建了一个线程:
public void ProcessRequest(HttpContext context)
{
string xmlFileNameParam = ...
string bakFileName = ...
ParameterizedThreadStart ThreadWithParam = dbFuncForThread;
Thread thread = new Thread(ThreadWithParam);
thread.IsBackground = false;
dynamic d = new ExpandoObject();
d.XmlFileNameParam = xmlFileNameParam;
d.BakFileName = bakFileName;
thread.Start(d);
}
public void dbFuncForThread(dynamic d)
{
// activate the long sp
}
但是过了一会儿,我在 EventLog 中看到此错误(并且操作停止):**
存在问题,第 259Thread 行被中止。 在 System.Threading.Thread.SleepInternal(Int32 毫秒超时)*
为什么?
我给了线程所需的所有环境(连接超时、执行超时)
PS 如果我不使用线程并放置像这样的正常代码(尽管 UI 被阻止):
public void ProcessRequest(HttpContext context)
{
string xmlFileNameParam = ...
string bakFileName = ...
dynamic d = new ExpandoObject();
d.XmlFileNameParam = xmlFileNameParam;
d.BakFileName = bakFileName;
dbFuncForthread(d);
}
public void dbFuncForThread(dynamic d )
{
// activate the long sp...
}
那么代码就很好,一切都很好。这是如何以及为什么以及我该如何解决它?
I've created a asp.net page which execute a very long sp (about 1 hour of sp execution time in SSMS).
there is an Admin.aspx which activate this SP.
in web.config I've added:
<httpRuntime maxRequestLength="111264" requestValidationMode="2.0" executionTimeout="10000000" />
also in the connection properties Ive added :
_cmd.CommandTimeout = 0; // unlimited
the function is using jquery ajax (to ashx) to activate the sp (it is working and fine)
the jquery calls an ashx file which activate the sp .
However I didn't want the the UI will be blocked till the end of the operation so i created a thread in the ashx file :
public void ProcessRequest(HttpContext context)
{
string xmlFileNameParam = ...
string bakFileName = ...
ParameterizedThreadStart ThreadWithParam = dbFuncForThread;
Thread thread = new Thread(ThreadWithParam);
thread.IsBackground = false;
dynamic d = new ExpandoObject();
d.XmlFileNameParam = xmlFileNameParam;
d.BakFileName = bakFileName;
thread.Start(d);
}
public void dbFuncForThread(dynamic d)
{
// activate the long sp
}
However after a while , i see this error in the EventLog (and the operation stopped):**
There is a problem line 259Thread was being aborted.
at System.Threading.Thread.SleepInternal(Int32 millisecondsTimeout)*
Why?
I gave the thread all the environment it needed (connection timeout, execution timeout)
P.S.
If I don't use a thread and put a normal code like this (UI Is blocked though):
public void ProcessRequest(HttpContext context)
{
string xmlFileNameParam = ...
string bakFileName = ...
dynamic d = new ExpandoObject();
d.XmlFileNameParam = xmlFileNameParam;
d.BakFileName = bakFileName;
dbFuncForthread(d);
}
public void dbFuncForThread(dynamic d )
{
// activate the long sp...
}
So the code is fine and everything is fine. How and why is that and how do I solve it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为你可能会重新考虑你的设计,更好地使用Windows服务来为你做到这一点。从Web服务将数据传递到Windows服务,然后执行sp。在 Web 环境中,这种情况的明显问题可能是 IISReset 或 appdomain 回收将中止所有线程。
I think you may reconsider your design, better use windows service to do this for you. From webservice pass data to windows service which thn execute sp. The obvious problem for this scenario in web environment couldbe IISReset or appdomain recycle will abort all threads.
这看起来线程不喜欢等待这么长时间才能完成 SP,这不是 SQL 调用超时。您可以异步调用它,而不是显式启动一个新线程来调用 SP: 在 C# 中异步调用 SQL Server 存储过程 ?
This looks like the thread doesn't like having to wait so long for the SP to complete, it isn't a SQL call time-out. Rather than explicitly starting a new thread to call the SP, can you just call it asynchronously: Asynchronous call of a SQL Server stored procedure in C# ?