Plip's Weblog

Phil Winstanley - British Microsoft ASP.NET MVP & ASP Insider.

Multi Threading in ASP.NET

One of the applications I'm writing needs to call web services to sen data off to a third part, however the user doesn't need to see any kind of response from these web services, so what I wanted to do was fire off the web service calls in a different thread and return the page to the user while the web services are still being called.

This was great, until I came to need something from the HttpContext in my web service call, when using threads in ASP.NET the HttpContext is only avalibale to the main thread, and not to any child threads without some clever trickery.

Here's the methodology I've used to sort it: -

(Source:  http://www.codecomments.com/ASP_.NET/message351969.html )

 

MyThreadClass T = new MyThreadClass();

T.User = System.Web.HttpContext.Current.User.Identity.Name;

T.StartThread();

 

 

using System;

using System.Threading;

 

namespace Private

{

          public class MyThreadClass

          {

 

                   //Public Property to store the Username

                   private string _User = String.Empty;

                   public string User

                   {

                             get

                             {

                                      return _User;

                             }

                             set

                             {

                                      _User = value;

                             }

                   }

 

                   //Public method to start the Thread going.

                   public void StartThread()

                   {

                             System.Threading.Thread T = new System.Threading.Thread(new System.Threading.ThreadStart(Run));

                             T.Start();

                   }

 

                   //Private method that actuall does stuff, in this case e-mails the User property to me.

                   private void Run()

                   {                  BusinessProcesses.Emails.Send("phil@winstanley.name","phil@winstanley.name",String.Empty,"Thread",_User,System.Web.Mail.MailFormat.Text);

                   }

 

                   public MyThreadClass()

                   {

                   }

          }

}

 

Posted: Jan 20 2005, 10:48 PM by Plip | with 12 comment(s)
Filed under: ,

Comments

Jeff said:

The way I do this is to drop whatever data it is I need to send into a database table (a journal, I suppose). I set a timer in an HttpModule to fire off the data every so often. It's a lot easier to reference the application context this way. For something simple, as with your case, your example is even easier still.
# January 20, 2005 9:56 PM

mikey said:

but how do you kill the thread. I don't think the thread will stop when session ends, that could leave orphans threads
# February 16, 2005 6:00 PM

mikey said:

but how do you kill the thread when it finished. I don't think the thread will stop when session ends, that could leave orphans threads
# February 16, 2005 6:01 PM

Yasser said:

Threads has to die after the execution of the process in normal situations, occasionally it is required for the programmer to kill a thread.

# March 12, 2007 11:04 AM

Quang Nguyen said:

Hi,

My situation is: in my Webpage, we need to write down information of user activities in to a log file. It would be better if writing down action happens in separated thread. This thread will not belong to any thread. How can I do?

Thanks

# May 22, 2007 6:19 AM

Ariane said:

In your "Run" method, what if you wanted to specify client session information?  (e.g. if the email address in your example was different everytime).  

I am looking to pass session variables into a thread (that runs as as the intranet user - in web.config <identity impersonate=true/>).  An error happens when I pass the Session Variables (I created in C# class facade) does not pass into the thread that I created similar to your "Run" method. Any suggestions?

A side note.  To get around orphan threads, can you just declare the thread as an object (e.g. wi), do the stuff you want with that object, then just write wi.Undo?

# July 13, 2007 11:20 AM

Raidu said:

Use the ParameterizedThreadStart and pass the session data as parameters to Thread.Start() method.

Thread oProcessThread = new Thread(new ParameterizedThreadStart(MethodToCall));

oProcessThread.Priority = ThreadPriority.Lowest;

oProcessThread.Start(new string[] {Session.["SessionData1"].ToString(), Session.["SessionData2"].ToString()});

# July 18, 2007 1:05 PM

Navdeep said:

In the run method m not able to get the Buissness Processes . Do i need to add the namespace for that???

thx in advance

# July 23, 2007 3:27 AM

Semil said:

<a href= spiritez.com ></a>

# December 5, 2008 9:12 PM

ellaelax-hw said:

<a href= membres.lycos.fr/dertull >zx10r graphics</a>

# December 26, 2008 11:37 AM

Larcik-zb said:

<a href= http://adultdatingssfinder.com >chat online</a>

# March 1, 2009 2:25 AM
Leave a Comment

(required) 

(required) 

(optional)

(required)