multithreading in web apps?

E

Edward W.

Can you do multi-threading in web applications? I basically understand how
to do it in winforms but am not sure about it in web apps. Can someone
point me to to an exmaple or an article that shows how?
 
J

Jeffrey Palermo [MCP]

Edward,
Here is one article on this topic:
http://msdn.microsoft.com/msdnmag/issues/03/06/Threading/default.aspx

I'd also like to say that multi-threading in ASP.NET is very similar to
doing it in a Windows app because you are executing .Net code in both
instances. If you just want to kick of a process and return the page
immediately, check out this sample I whipped up:

private void Page_Load(object sender, System.EventArgs e)

{

Thread thd = new Thread(new ThreadStart(LongRunning));

thd.Start();

//this.LongRunning();

Response.Write("Done " + DateTime.Now.ToString());

}



private void LongRunning()

{

Thread.Sleep(5000);

}
 
J

John Saunders

Edward W. said:
Can you do multi-threading in web applications? I basically understand
how to do it in winforms but am not sure about it in web apps. Can
someone point me to to an exmaple or an article that shows how?

You should not attempt multithreading in web applications, except in very
limited circumstances. This has to do with the totally different lifecycle
between a web application and a winforms application. In particular, a Page
is an object which exists only for the duration of a request. When the
request is over, the page is gone. If you start a thread from that Page, it
should expect to execute after the page, and the request, are gone.

Leave multithreading out of web applications unless you have no other choice
or are an expert.

John Saunders
 
J

Jeffrey Palermo [MCP]

I agree that the need will rarely come up. One such need is if several
operations need to take place, and each one might take a bit of time. In
that case, all four could be started on separate threads, and the page could
return after all four threads complete their work. Of course, designing a
page like that would require more than intermediate skill with ASP.NET.

--
Best regards,
Jeffrey Palermo
Blog: http://dotnetjunkies.com/weblog/jpalermo
 
Joined
May 26, 2010
Messages
1
Reaction score
0
I agree that the need will rarely come up.

I have this need all the time, why do we have ajax web applications?

You can always use javascript to make Ajax calls that act just like threads.

Babloome
 

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

Forum statistics

Threads
473,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top