问题描述
有关一些奇怪的原因,我不能在我的生产环境中运行的任何职能作为一个新的线程,即使它正常工作在本地和精细的临时服务器,它具有相同的规格作为生产服务器(Windows 2003中,IIS 6)
For some strange reason, I can't run any functions as a new thread in my production environment, even though it works fine locally and fine on the staging server, which has identical specs as the production server (Windows 2003, IIS 6)
下面是我的code:
System.Threading.Thread th = new System.Threading.Thread(TestFunction);
th.Start();
这是函数:
public void TestFunction()
{
LogUtility.Log("hello world");
}
th.Start之后,没有别的访问。有一些可能会导致此行为设置? th.ThreadState正在恢复跑为TestFunction的时间。
After th.Start, nothing else is accessed. Is there some setting that could cause this behavior? th.ThreadState is returning "running" for the duration of TestFunction.
(LogUtility.Log刚刚写入文本文件)
(LogUtility.Log just writes the text to a file)
编辑:这其实工作之前,它只是停止工作冒出
This actually worked before and it just stopped working out of nowhere.
推荐答案
原来那是因为我用的是模拟,并创建一个新的线程时模拟不适用。这是为了让线程使用相同的假冒我怎么固定它的调用函数:
Turns out it is because I was using impersonation and impersonation doesn't apply when creating a new thread. This is how I fixed it so that the thread is using the same impersonation as the calling function:
public static WindowsIdentity ident;
public void ProcessRequest(HttpContext context)
{
ident = WindowsIdentity.GetCurrent();
System.Threading.Thread th = new System.Threading.Thread(ThreadedFunction);
th.Start();
}
public void ThreadedFunction()
{
WindowsImpersonationContext c = null;
try
{
c = ident.Impersonate();
// Your code here
}
finally
{
if (c != null) c.Undo();
}
}
这篇关于ASP.NET线程不工作在生产的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!