473,396 Members | 2,004 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

can i use threads in asp.net?

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


Nov 19 '05 #1
10 1389
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.

--
Scott
http://www.OdeToCode.com/blogs/scott/
On Tue, 15 Nov 2005 09:57:51 -0500, "Dica" <ge*****@hotmail.com>
wrote:
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



Nov 19 '05 #2
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?
--
--Dr. Abdel
"Scott Allen" wrote:
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.

--
Scott
http://www.OdeToCode.com/blogs/scott/
On Tue, 15 Nov 2005 09:57:51 -0500, "Dica" <ge*****@hotmail.com>
wrote:
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



Nov 19 '05 #3
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.
"Dr. Abdel." <Dr******@somewhere.com> skrev i meddelandet news:C4**********************************@microsof t.com...
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?
--
--Dr. Abdel
"Scott Allen" wrote:
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.

--
Scott
http://www.OdeToCode.com/blogs/scott/
On Tue, 15 Nov 2005 09:57:51 -0500, "Dica" <ge*****@hotmail.com>
wrote:
>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
>
>
>
>
>


Nov 19 '05 #4

"Dr. Abdel." <Dr******@somewhere.com> wrote in message
news:C4**********************************@microsof t.com...
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 like someone to answer it!

How to make the server reload the page when the background processing is
done?
--
--Dr. Abdel
"Scott Allen" wrote:
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.

--
Scott
http://www.OdeToCode.com/blogs/scott/
On Tue, 15 Nov 2005 09:57:51 -0500, "Dica" <ge*****@hotmail.com>
wrote:
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



Nov 19 '05 #5
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/is.../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....
--
--Dr. Abdel
"Dica" wrote:

"Dr. Abdel." <Dr******@somewhere.com> wrote in message
news:C4**********************************@microsof t.com...
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
like someone to answer it!

How to make the server reload the page when the background processing is
done?
--
--Dr. Abdel
"Scott Allen" wrote:
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.

--
Scott
http://www.OdeToCode.com/blogs/scott/
On Tue, 15 Nov 2005 09:57:51 -0500, "Dica" <ge*****@hotmail.com>
wrote:

>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
>
>
>
>
>


Nov 19 '05 #6
On Tue, 15 Nov 2005 11:56:03 -0800, "Dr. Abdel."
<Dr******@somewhere.com> wrote:
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.

--
Scott
http://www.OdeToCode.com/blogs/scott/

Nov 20 '05 #7
On Tue, 15 Nov 2005 15:53:52 -0500, "Dica" <ge*****@hotmail.com>
wrote:
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.

--
Scott
http://www.OdeToCode.com/blogs/scott/

Nov 20 '05 #8
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!

Scott wrote:
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?!
--
--Dr. Abdel
"Scott Allen" wrote:
On Tue, 15 Nov 2005 11:56:03 -0800, "Dr. Abdel."
<Dr******@somewhere.com> wrote:
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.

--
Scott
http://www.OdeToCode.com/blogs/scott/

Nov 20 '05 #9
>
I don't see how we can do this with client scripting?! may be I am missing
smthg?!


Yes, you can use client scripting to display loading message, progress
bars, etc. The client script checks with the server to see when the
results are available, and when the results are ready it can write
them into the page.

Here are some examples:

http://www.schwarz-interactive.de/Ex...onloading.aspx
http://www.informit.com/articles/art...74363&seqNum=2

--
Scott
http://www.OdeToCode.com/blogs/scott/

Nov 20 '05 #10
That is cool!
especially the article that explain how it is done.
Thx scott.
--
--Dr. Abdel
"Scott Allen" wrote:

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


Yes, you can use client scripting to display loading message, progress
bars, etc. The client script checks with the server to see when the
results are available, and when the results are ready it can write
them into the page.

Here are some examples:

http://www.schwarz-interactive.de/Ex...onloading.aspx
http://www.informit.com/articles/art...74363&seqNum=2

--
Scott
http://www.OdeToCode.com/blogs/scott/

Nov 20 '05 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Ronan Viernes | last post by:
Hi, I have created a python script (see below) to count the maximum number of threads per process (by starting new threads continuously until it breaks). ###### #testThread.py import...
0
by: Al Tobey | last post by:
I was building perl 5.8.2 on RedHat Enterprise Linux 3.0 (AS) today and noticed that it included in it's ccflags "-DTHREADS_HAVE_PIDS." I am building with -Dusethreads. With newer Linux...
6
by: m | last post by:
Hello, I have an application that processes thousands of files each day. The filenames and various related file information is retrieved, related filenames are associate and placed in a linked...
34
by: Kovan Akrei | last post by:
Hi, I would like to know how to reuse an object of a thread (if it is possible) in Csharp? I have the following program: using System; using System.Threading; using System.Collections; ...
3
by: bygandhi | last post by:
Hi - I am writing a service which will check a process and its threads for their state ( alive or dead ). The process has 5 .net managed threads created using thread.start and each have been...
10
by: [Yosi] | last post by:
I would like to know how threads behavior in .NET . When an application create 4 threads for example start all of them, the OS task manager will execute all 4 thread in deterministic order manes,...
6
by: RahimAsif | last post by:
Hi guys, I would like some advice on thread programming using C#. I am writing an application that communicates with a panel over ethernet, collects data and writes it to a file. The way the...
3
by: mjheitland | last post by:
Hi, I like to know how many threads are used by a Threading.Timer object. When I create a Threading.Timer object calling a short running method every 5 seconds I expected to have one additional...
10
by: Darian | last post by:
Is there a way to find all the thread names that are running in a project? For example, if I have 5 threads T1, T2, T3, T4, T5...and T2, T4, and T5 are running...I want to be able to know that...
4
by: tdahsu | last post by:
All, I'd appreciate any help. I've got a list of files in a directory, and I'd like to iterate through that list and process each one. Rather than do that serially, I was thinking I should...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.