Thread from code behind aspx
-
hello Can I start a thread from aspx code behind (long running stored proc call) Thread SomeThread = new Thread(this.RunLongRunningCommand); SomeThread.Start(SomeArg); I'm worried aspx page worker's thread life cycle.
dev
Check this 1. [Threading in ASP.NET] 2. [Multi-Threading in ASP.NET]
-
hello Can I start a thread from aspx code behind (long running stored proc call) Thread SomeThread = new Thread(this.RunLongRunningCommand); SomeThread.Start(SomeArg); I'm worried aspx page worker's thread life cycle.
dev
As an alternative to using System.Threading, which I have to admit I find a difficult class to get to grips with, can I suggest an alternative method for executing a long running process (LRP) within as ASP.NET appliucation... ..this is only of any real use when you can set up your LRP in such a way that you can execute it by passing just an ID number (say to a database record), or at least only a relatively small amount of data to it via a QueryString. Set up your LRP within a generic handler, say "lrp.ashx" Them, in your webapage when you want to execute this, add:
Try Dim request As System.Net.HttpWebRequest = DirectCast(System.Net.WebRequest.Create(HttpPathOf("lrp.ashx")), System.Net.HttpWebRequest) request.Method = "POST" request.ContentType = "application/x-www-form-urlencoded" Dim postData As String = "id=" & nid.ToString ' for example - set up whatever data you need to post to the handler here request.ContentLength = postData.Length Dim writer As New System.IO.StreamWriter(request.GetRequestStream(), System.Text.Encoding.ASCII) writer.Write(postData) writer.Close() Catch ex As Exception ' whatever End Try
Note: by HttpPathOf("lrp.ashx") I mean the full URL (ie starting "http://www.mydomain...." to lrp.ashx - you can't just use a relative URL or Server.MapPath The above code will execute lrp.ashx in a new process, adn you can (for example) immediately redirect to another page while it is still executing, adn yor users will never know.... Perhaps someone here willl tell me why I shouldn't be doiung this, but I've found it works very well :-) And no, I can't translate it into C#! ;P