473,782 Members | 2,419 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(B yVal sender As System.Object, ByVal e As
System.EventArg s) Handles Button1.Click

Dim t As Thread

t = New Thread(AddressO f Me.BackgroundPr ocess)

t.Start()

End Sub

Private Sub BackgroundProce ss()

Dim i As Integer = 1

Do While True

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

i += 1

Loop

End Sub


Nov 19 '05 #1
10 1417
By the time the second thread starts executing in BackgroundProce ss,
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*****@hotmai l.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(B yVal sender As System.Object, ByVal e As
System.EventAr gs) Handles Button1.Click

Dim t As Thread

t = New Thread(AddressO f Me.BackgroundPr ocess)

t.Start()

End Sub

Private Sub BackgroundProce ss()

Dim i As Integer = 1

Do While True

ListBox1.Items .Add("Iteration s: " + 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 BackgroundProce ss,
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*****@hotmai l.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(B yVal sender As System.Object, ByVal e As
System.EventAr gs) Handles Button1.Click

Dim t As Thread

t = New Thread(AddressO f Me.BackgroundPr ocess)

t.Start()

End Sub

Private Sub BackgroundProce ss()

Dim i As Integer = 1

Do While True

ListBox1.Items .Add("Iteration s: " + 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 "asynchrono us pages", "script callbacks", "ajax"

PL.
"Dr. Abdel." <Dr******@somew here.com> skrev i meddelandet news:C4******** *************** ***********@mic rosoft.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 BackgroundProce ss,
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*****@hotmai l.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(B yVal sender As System.Object, ByVal e As
>System.EventAr gs) Handles Button1.Click
>
>Dim t As Thread
>
>t = New Thread(AddressO f Me.BackgroundPr ocess)
>
>t.Start()
>
>End Sub
>
>Private Sub BackgroundProce ss()
>
>Dim i As Integer = 1
>
>Do While True
>
>ListBox1.Items .Add("Iteration s: " + i.ToString)
>
>i += 1
>
>Loop
>
>End Sub
>
>
>
>
>


Nov 19 '05 #4

"Dr. Abdel." <Dr******@somew here.com> wrote in message
news:C4******** *************** ***********@mic rosoft.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.EventArg s) Handles MyBase.Load

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

If Request.QuerySt ring("pg") <> "" Then

sThisPage = Request.QuerySt ring("pg")

If IsNumeric(sThis Page) Then

iNextPage = CInt(sThisPage) + 1

End If

Else

iNextPage = 1

End If

Dim oNextPage As New NextSlideShowPa ge(iNextPage)

'// set up thread to do pausing //

Dim t As Thread

t = New Thread(AddressO f oNextPage.doNot hing)

t.Start()

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

oNextPage.getNe xtPage()

End Sub

here's the NextSlideSHowPa ge class:

Public Class NextSlideShowPa ge

Inherits System.Web.UI.P age

Private iNextPage As Integer

Sub New(ByVal iPage As Integer)

iNextPage = iPage

End Sub

Sub getNextPage()

Response.Redire ct("slide_show. aspx?pg=" & iNextPage.ToStr ing)

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 BackgroundProce ss,
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*****@hotmai l.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(B yVal sender As System.Object, ByVal e As
System.EventAr gs) Handles Button1.Click

Dim t As Thread

t = New Thread(AddressO f Me.BackgroundPr ocess)

t.Start()

End Sub

Private Sub BackgroundProce ss()

Dim i As Integer = 1

Do While True

ListBox1.Items .Add("Iteration s: " + 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******@somew here.com> wrote in message
news:C4******** *************** ***********@mic rosoft.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.EventArg s) Handles MyBase.Load

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

If Request.QuerySt ring("pg") <> "" Then

sThisPage = Request.QuerySt ring("pg")

If IsNumeric(sThis Page) Then

iNextPage = CInt(sThisPage) + 1

End If

Else

iNextPage = 1

End If

Dim oNextPage As New NextSlideShowPa ge(iNextPage)

'// set up thread to do pausing //

Dim t As Thread

t = New Thread(AddressO f oNextPage.doNot hing)

t.Start()

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

oNextPage.getNe xtPage()

End Sub

here's the NextSlideSHowPa ge class:

Public Class NextSlideShowPa ge

Inherits System.Web.UI.P age

Private iNextPage As Integer

Sub New(ByVal iPage As Integer)

iNextPage = iPage

End Sub

Sub getNextPage()

Response.Redire ct("slide_show. aspx?pg=" & iNextPage.ToStr ing)

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 BackgroundProce ss,
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*****@hotmai l.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(B yVal sender As System.Object, ByVal e As
>System.EventAr gs) Handles Button1.Click
>
>Dim t As Thread
>
>t = New Thread(AddressO f Me.BackgroundPr ocess)
>
>t.Start()
>
>End Sub
>
>Private Sub BackgroundProce ss()
>
>Dim i As Integer = 1
>
>Do While True
>
>ListBox1.Items .Add("Iteration s: " + i.ToString)
>
>i += 1
>
>Loop
>
>End Sub
>
>
>
>
>


Nov 19 '05 #6
On Tue, 15 Nov 2005 11:56:03 -0800, "Dr. Abdel."
<Dr******@somew here.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(AddressO f BackgroundProce ss)
t.Start()
t.Join() ' this will block until the thread is finished
MyLabel.Text = "Done!"

End Sub

Private Sub BackgroundProce ss()

Dim i As Integer = 1
Thread.Sleep(10 )
Do While i < 100
MyListBox.Items .Add("Iteration s: " + 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*****@hotmai l.com>
wrote:
what i actually want to do is create a slide show to
automaticall y 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...che ck 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******@somew here.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(AddressO f BackgroundProce ss)
t.Start()
t.Join() ' this will block until the thread is finished
MyLabel.Text = "Done!"

End Sub

Private Sub BackgroundProce ss()

Dim i As Integer = 1
Thread.Sleep(10 )
Do While i < 100
MyListBox.Items .Add("Iteration s: " + 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

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

Similar topics

3
5402
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 thread, sys
0
2011
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 distributions using the Native Posix Threading Layer (NPTL), this isn't entirely true anymore and is AFAIK unsupported (using a pid to signal/identify threads). I removed it from my config.sh script and ran Configure -der. make test runs just fine. ...
6
3206
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 list within a single object, which is then placed on a stack(This cuts down thread creation and deletions roughly by a factor of 4). I create up to 12 threads, which then process a single object off of the stack. I use a loop with a boolean...
34
10813
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; public class A {
3
22177
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 assigned a name. As in .net there is no way we can get the managed threads, I am thinking of making a win32 call and check their status.
10
1682
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, OS execute (All have same priority) Thread#1 may be other threads, Thread#2 may be other threads, Thread#3 may be other threads,
6
2527
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 data is collected is that we have different schedules (so one set of data is collected say every second, another set of data might be collected every 30 seconds, and so on).
3
5974
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 ThreadPool thread. And that is exactly what MS VIsual Studio shows. But when I run Processexplorer or Taskmanager I see 2 additional threads, after a while another 2 additional threads. With the 3 threads at start time we have totally 7 threads.
10
1762
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 T2, T4 and T5 are already running. Thanks, Darian
4
2258
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 start five threads and process five files at a time. Is this a good idea? I picked the number five at random... I was thinking that I might check the number of processors and start a multiple of that, but then I remembered KISS and it seemed that...
0
9643
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10313
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10147
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9946
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8968
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6735
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5378
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5511
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4044
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.