can i use threads in asp.net?


D

Dica

i've used threads in a couple of c# desktop apps with no problems before,
but can't seem to get this code to update my listBox:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click

Dim t As Thread

t = New Thread(AddressOf Me.BackgroundProcess)

t.Start()

End Sub

Private Sub BackgroundProcess()

Dim i As Integer = 1

Do While True

ListBox1.Items.Add("Iterations: " + i.ToString)

i += 1

Loop

End Sub
 
Ad

Advertisements

S

Scott Allen

By the time the second thread starts executing in BackgroundProcess,
the original thread has finished executing all the code needed to
render the page, and the page is done.

ASP.NET is already multi-threaded. Chances are you'll hurt scalability
adding your own threads.
 
G

Guest

Scott,
while you are correct in the explanation you give Dica about why he does
not see any effect, you didn't give him how to make it work.

Also "you'll hurt scalability adding your own threads." is not always
correct, there are situations when you need to submit a search (that is
taking too long) to work in the background....

What he is trying to accomplish may not make sense in the trivial example he
is using to explain his needs, but it is a legitime question, that I would
like someone to answer it!

How to make the server reload the page when the background processing is
done?
 
P

PL

In ASP.NET 2.0 you have the notion of asynchronous pages where you
can fire off and do some task while the requesting thread continues with
something else, however care must be taken to use it correctly otherwise you'll
waste two threads instead of one.

You can also use AJAX like functionality to update your pages, if you have
a long running task you can check if it's done in certain intervals and then update
when it is done.

Do a google search on "asynchronous pages", "script callbacks", "ajax"

PL.
 
D

Dica

Dr. Abdel. said:
Scott,
while you are correct in the explanation you give Dica about why he does
not see any effect, you didn't give him how to make it work.

Also "you'll hurt scalability adding your own threads." is not always
correct, there are situations when you need to submit a search (that is
taking too long) to work in the background....

What he is trying to accomplish may not make sense in the trivial example he
is using to explain his needs,

actually, yes, that's true, though the example i provided may not have made
much sense. i was trying to keep things as simple as possible while trying
to figure out basic threading in a asp.net environment and the example i
posted was from a vb.net windows form example i found. sorry for any
confusion. what i actually want to do is create a slide show to
automatically forward the user to the next page after 5 seconds:

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

'// get the page number of the slide show //

If Request.QueryString("pg") <> "" Then

sThisPage = Request.QueryString("pg")

If IsNumeric(sThisPage) Then

iNextPage = CInt(sThisPage) + 1

End If

Else

iNextPage = 1

End If

Dim oNextPage As New NextSlideShowPage(iNextPage)

'// set up thread to do pausing //

Dim t As Thread

t = New Thread(AddressOf oNextPage.doNothing)

t.Start()

t.Sleep(5000) '// wait 5 seconds before advancing to next page //

oNextPage.getNextPage()

End Sub



here's the NextSlideSHowPage class:



Public Class NextSlideShowPage

Inherits System.Web.UI.Page

Private iNextPage As Integer

Sub New(ByVal iPage As Integer)

iNextPage = iPage

End Sub

Sub getNextPage()

Response.Redirect("slide_show.aspx?pg=" & iNextPage.ToString)

End Sub

Sub doNothing()

End Sub

End Class



this almost seems to work except i'm receiving a 'Response is not available
in this context' error message.



thoughts?


but it is a legitime question, that I would
 
G

Guest

Dica,
you are in luck today! I was just browsing the latest issue of MSDN Mag. and
as PL pointed out, asyn pages is what you need, and it is explained in the
latest issue....

As PL said this is natively supported in ASP.NET 2.0, but it can be done
(with some effort) in ASP.NET 1.x as explained in this article:
http://msdn.microsoft.com/msdnmag/issues/03/06/Threading/

Basically since every page will run in its own thread (and that thread will
not wait for your custom thread) you make the page first run your own thread
then when it is finished it will let the page run as normal!

Read the article you will understand....
 
Ad

Advertisements

S

Scott Allen

Scott,
while you are correct in the explanation you give Dica about why he does
not see any effect, you didn't give him how to make it work.

Good point.

Just two tweaks:
1) Get rid of the inifinite loop.
2) Have Page_Load wait for the thread to complete it's work.

Private Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) _
Handles MyBase.Load

Dim t As New Thread(AddressOf BackgroundProcess)
t.Start()
t.Join() ' this will block until the thread is finished
MyLabel.Text = "Done!"

End Sub

Private Sub BackgroundProcess()

Dim i As Integer = 1
Thread.Sleep(10)
Do While i < 100
MyListBox.Items.Add("Iterations: " + i.ToString)
i += 1
Loop

End Sub


Also "you'll hurt scalability adding your own threads." is not always
correct, there are situations when you need to submit a search (that is
taking too long) to work in the background....

In the case above there are two threads needed to process one request,
so it only creates more overhead. Async pages in 2.0 are different
because they free up the original thread to process more requests.

What he is trying to accomplish may not make sense in the trivial example he
is using to explain his needs, but it is a legitime question, that I would
like someone to answer it!

How to make the server reload the page when the background processing is
done?

Ah, in that case you have to do some work on the client. Either use
META tags to refresh the page or use JavaScript / AJAX from the client
to check on processing. The server can't force the client's browser to
reload a page - the client has to initiate the request.
 
S

Scott Allen

what i actually want to do is create a slide show to
automatically forward the user to the next page after 5 seconds:

In that case you don't need to fiddle with threads or async pages.
You'll want to use client side script / JavaScript / AJAX and have the
client make a request to the server. The server can't reach out and
get to the client - the browser always has to initiate the request.
 
G

Guest

Scott,
excellent answers (excep the last one, I will comment on it below).

Dica,
for slide show, the best solution and (faster) is JavaScript...in fact it is
readily available...check out: http://www.needscripts.com/ and type "slide
show" in search button, you will find slide show that advance automatically
or those that needs a button to be clicked!
Ah, in that case you have to do some work on the client. Either use
META tags to refresh the page or use JavaScript / AJAX from the client
to check on processing. The server can't force the client's browser to
reload a page - the client has to initiate the request.

This is true in the case of Dica, but not the example I gave....I am aware
of META tag refresh and Javascript, but here is the problem we have been
faced with few months ago.

When user navigate to a page, we need the page to render all element, except
one portion that is tied to doing a search and return a result.

So the user should see all the page normally except for one table that will
display "Loading..." then when the result are ready (no user interaction
needed), the search result sould fill the table instead of the "Loading..."

I don't see how we can do this with client scripting?! may be I am missing
smthg?!
 
S

Scott Allen

Ad

Advertisements

Ad

Advertisements


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

Top