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)
Here is my code:
System.Threading.Thread th = new System.Threading.Thread(TestFunction);
th.Start();
And this is the function:
public void TestFunction()
{
LogUtility.Log("hello world");
}
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 just writes the text to a file)
EDIT: This actually worked before and it just stopped working out of nowhere.
-
Could this be security related? The threading operations require rather high CAS (Code Access Security) privileges and in a hosting environment you may be running restricted privileges.
-
Are you sure that it's not just the LogUtility failing? I wouldn't be so quick to suspect the threading system when a much simpler explanation exists :).
Arthur Chaparyan : The LogUtility is doing a File.AppendAllText("log.txt"). I tried to make it do something as simple as possible to eliminate it as a reason for the error.MichaelGG : Well, I don't consider performing file IO to be "as simple as possible"...Arthur Chaparyan : Is there anything simpler you would suggest? I can't output anything since it's on a separate thread, so I would need something that I can check afterwardsMichaelGG : Perhaps setting a variable or a reset event? -
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(); } }
MichaelGG : What's impersonation have to do with the thread not working?Arthur Chaparyan : The account the thread was running on did not have permissions to run anything since the web files are on a share
0 comments:
Post a Comment