Unable to start thread from ASP.net app with windows server 2003

T

tonyreynolds

I have an asp.net application that needs to spawn a new thread but
return the response back to the user before the thread finishes. My
application works fine on my machine (XP pro) and on a win 2000 server,
but I can't get a thread to start on a new win 2003 server. I've
posted some sample code below. I've made sure that the user this runs
under has permission to write the test file to the c: drive. This code
works on all the servers if I don't start a new thread but call the
WriteFile method directly.

I'm sure I've missed a security setting somewhere, I know 2003 is
supposed to be tied down a little tighter than it's predecessors.
Any help would be appreciated. Thanks in advance!

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Threading;
using System.IO;

namespace TestApp
{
/// <summary>
/// Summary description for Test.
/// </summary>
public class Test : System.Web.UI.Page
{
private void Page_Load(object sender, System.EventArgs e)
{
Response.Write("beginning thread<br>");
ThreadStart ts = new ThreadStart(WriteFile);
Thread t = new Thread(ts);
t.Start();
Response.Write("began thread<br>");
}

private void WriteFile()
{
StreamWriter sw = File.CreateText("c:\\test.txt");
sw.WriteLine("test - "+ DateTime.Now.ToLongTimeString());
sw.Close();
}

#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
}
}
 
T

tonyreynolds

Just wanted to add - I don't get any errors, the application appears to
run normally, but it won't write that file, so I assume the thread is
never really created or it silently fails somehow.
 
J

John Saunders

I have an asp.net application that needs to spawn a new thread but
return the response back to the user before the thread finishes. My
application works fine on my machine (XP pro) and on a win 2000 server,
but I can't get a thread to start on a new win 2003 server. I've
posted some sample code below. I've made sure that the user this runs
under has permission to write the test file to the c: drive. This code
works on all the servers if I don't start a new thread but call the
WriteFile method directly.

Are you aware that WriteFile may well be executing after the request is
complete? At that time, the Page, Request, etc. are all garbage. This could
have something to do with your problem.

However, the next step would be for you to put a try-catch block around the
code in WriteFile and find out what if any exception is being thrown. Same
with Page_Load.

John Saunders
 
W

William F. Robertson, Jr.

Will it be garbage as there is a root to the WriteFile method that would
keep the page around longer than necessary?

bill
 
J

John Saunders

William F. Robertson said:
Will it be garbage as there is a root to the WriteFile method that would
keep the page around longer than necessary?

It can be whatever ASP.NET wants it to be. The point is, ASP.NET has no
support for accessing these resources after the request has completed. It
would be irresponsible to depend on whether the data happen not to be trash
in this particular release of ASP.NET.

The general solution is, "don't do that". Understand that such data may not
be accessed from a separate thread, and make sure the thread doesn't access
it.

John Saunders
 
T

tonyreynolds

Ok, I fixed it and I'm not sure why. My issue was not in creating the
thread, but a file access permission problem. I did the try catch
block in WriteFile as John suggested and wrote the error out to a
variable which I could output to the page after doing a Join() to get
the thread back to the host. The strange thing is if I write to the
file without doing it in a separate thread it works fine, but I get an
access problem when I do it in a new thread. I changed the file to be
in one folder off the c drive, like this: c:\testfolder\test.txt and
that works fine.
So I still don't understand it completely, but this change fixed it.
Maybe server 2003 treats the root drive with different security
parameters than a folder, and it differs from xp and 2000 in this way.
Or, it treats spawned threads with different security privileges
somehow. Or, as John and William suggest, when the main request ends
and the thread is still going it loses access to the resources it used
originally which may have provided it access. That makes sense to me
but doesn't explain why it works with my fix. Oh well, I don't need to
write my log directly to the c drive anyway.
Thanks guys!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top