473,732 Members | 2,205 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Thread won't start?


I'm trying to use threading for the first time and can't get it to work.

(VB)

In the page I've got:

Sub Page_Load( sender As Object, e As EventArgs )
dim o as new Example.Test
o.CreateThread( )
End Sub
and in the Example namespace:

Public Class Test

Public Sub RunJob()
While True
WriteToLog( DateTime.UtcNow )
Thread.Sleep( 1000 * 10 )
End While
End Sub

Public Sub CreateThread()
Dim t As New Thread( AddressOf RunJob )
t.Start()
HttpContext.Cur rent.Response.W rite( t.ThreadState )
End Sub

End Class

The response is 'Unstarted' (unless I set the priority of the new thread to
AboveNormal or Highest, in which case it's 'Stopped') and nothing gets
written to the log file. If I call o.RunJob() instead of o.CreateThread( ) on
page loading then that routine works as I expect, i.e. the datetime is
written to the log file every 10 seconds until the request timesout after 90
seconds.

What am I doing wrong?

Nov 19 '05 #1
7 1521
The lifetime of a child thread is limited to the lifetime of the parent
thread. As an ASP.Net Page instance lasts for several milliseconds, your
thread is dying almost immediately, when the Page class is unloaded. Try
loading the class into Session State and spawning the thread from there,
where the object remains persistent. However, do not attempt to write to the
Response from Session State, as there is none. The Response is a member of
the Current HttpHandler.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
Everybody picks their nose,
But some people are better at hiding it.

"Microsoft" <noone@home> wrote in message
news:eA******** ******@TK2MSFTN GP14.phx.gbl...

I'm trying to use threading for the first time and can't get it to work.

(VB)

In the page I've got:

Sub Page_Load( sender As Object, e As EventArgs )
dim o as new Example.Test
o.CreateThread( )
End Sub
and in the Example namespace:

Public Class Test

Public Sub RunJob()
While True
WriteToLog( DateTime.UtcNow )
Thread.Sleep( 1000 * 10 )
End While
End Sub

Public Sub CreateThread()
Dim t As New Thread( AddressOf RunJob )
t.Start()
HttpContext.Cur rent.Response.W rite( t.ThreadState )
End Sub

End Class

The response is 'Unstarted' (unless I set the priority of the new thread
to AboveNormal or Highest, in which case it's 'Stopped') and nothing gets
written to the log file. If I call o.RunJob() instead of o.CreateThread( )
on page loading then that routine works as I expect, i.e. the datetime is
written to the log file every 10 seconds until the request timesout after
90 seconds.

What am I doing wrong?

Nov 19 '05 #2
When you create your child thread you need to realize that the HttpContext.Cur rent
isn't passed along to the other thread, so as Kevin said, you shouldn't be
accessing any of that information on your secondary thread. Also, once your
thread is started, again as Kevin pointed out, the ASP.NET thread has completed
processing of your page and has sent the result back to the client.

In your secondary thread you should be able to do the loop and write out
to a log file, though. I'd suggest debugging it to see what's going wrong
in your RunJob() method.

-Brock
DevelopMentor
http://staff.develop.com/ballen
I'm trying to use threading for the first time and can't get it to
work.

(VB)

In the page I've got:

Sub Page_Load( sender As Object, e As EventArgs )
dim o as new Example.Test
o.CreateThread( )
End Sub
and in the Example namespace:

Public Class Test

Public Sub RunJob()
While True
WriteToLog( DateTime.UtcNow )
Thread.Sleep( 1000 * 10 )
End While
End Sub
Public Sub CreateThread()
Dim t As New Thread( AddressOf RunJob )
t.Start()
HttpContext.Cur rent.Response.W rite( t.ThreadState )
End Sub
End Class

The response is 'Unstarted' (unless I set the priority of the new
thread to AboveNormal or Highest, in which case it's 'Stopped') and
nothing gets written to the log file. If I call o.RunJob() instead of
o.CreateThread( ) on page loading then that routine works as I expect,
i.e. the datetime is written to the log file every 10 seconds until
the request timesout after 90 seconds.

What am I doing wrong?


Nov 19 '05 #3
I'm not sure where you got this information, except perhaps you're confusing
this with other environments.
The lifetime of a child thread is limited to the lifetime of the
parent thread.
This is not correct. If a parent thread is terminated it does not implicitly
affect a child thread.
As an ASP.Net Page instance lasts for several
milliseconds, your thread is dying almost immediately, when the Page
class is unloaded.


ASP.NET threads are not created and terminated for each request nor are their
lifetimes tied to the lifetime of the page (or handler). Threads are are
drawn from the CLR thread pool to execute requests in ASP.NET. Once the request
is done the thread is returned to the CLR thread pool. The thread pool may,
over time, create and destroy threads, but this is based upon load upon the
server. It is not tied to each request and/or page.

-Brock
DevelopMentor
http://staff.develop.com/ballen

Nov 19 '05 #4


So all that HttpContext.Cur rent.Cache and
HttpContext.Cur rent.Request.Ma pPath stuff in the WriteToLog routine isn't
so clever when it's called by the new thread, then . . . !!

Still, should be possible to pass these values as properties of the Test
class.

Thanks for all your help,
Roger


"Brock Allen" <ba****@NOSPAMd evelop.com> wrote in message
news:10******** *************** @msnews.microso ft.com...
When you create your child thread you need to realize that the
HttpContext.Cur rent isn't passed along to the other thread, so as Kevin
said, you shouldn't be accessing any of that information on your secondary
thread. Also, once your thread is started, again as Kevin pointed out, the
ASP.NET thread has completed processing of your page and has sent the
result back to the client.
In your secondary thread you should be able to do the loop and write out
to a log file, though. I'd suggest debugging it to see what's going wrong
in your RunJob() method.

-Brock
DevelopMentor
http://staff.develop.com/ballen
I'm trying to use threading for the first time and can't get it to
work.

(VB)

In the page I've got:

Sub Page_Load( sender As Object, e As EventArgs )
dim o as new Example.Test
o.CreateThread( )
End Sub
and in the Example namespace:

Public Class Test

Public Sub RunJob()
While True
WriteToLog( DateTime.UtcNow )
Thread.Sleep( 1000 * 10 )
End While
End Sub
Public Sub CreateThread()
Dim t As New Thread( AddressOf RunJob )
t.Start()
HttpContext.Cur rent.Response.W rite( t.ThreadState )
End Sub
End Class

The response is 'Unstarted' (unless I set the priority of the new
thread to AboveNormal or Highest, in which case it's 'Stopped') and
nothing gets written to the log file. If I call o.RunJob() instead of
o.CreateThread( ) on page loading then that routine works as I expect,
i.e. the datetime is written to the log file every 10 seconds until
the request timesout after 90 seconds.

What am I doing wrong?


Nov 19 '05 #5
not quite true.

the lifetime of a thread is tied to the process. if the hosting appdomain is
unloaded, an abort request is sent to the thread by the clr

if you use create thread as the example did, a new process thread is
created, a thread is not taken from the clr thread pool. you need to use clr
thread pool api to do this.
-- bruce (sqlwork.com)
"Brock Allen" <ba****@NOSPAMd evelop.com> wrote in message
news:10******** *************** @msnews.microso ft.com...
I'm not sure where you got this information, except perhaps you're
confusing this with other environments.
The lifetime of a child thread is limited to the lifetime of the
parent thread.


This is not correct. If a parent thread is terminated it does not
implicitly affect a child thread.
As an ASP.Net Page instance lasts for several
milliseconds, your thread is dying almost immediately, when the Page
class is unloaded.


ASP.NET threads are not created and terminated for each request nor are
their lifetimes tied to the lifetime of the page (or handler). Threads are
are drawn from the CLR thread pool to execute requests in ASP.NET. Once
the request is done the thread is returned to the CLR thread pool. The
thread pool may, over time, create and destroy threads, but this is based
upon load upon the server. It is not tied to each request and/or page.

-Brock
DevelopMentor
http://staff.develop.com/ballen

Nov 19 '05 #6
> not quite true.

the lifetime of a thread is tied to the process. if the hosting
appdomain is unloaded, an abort request is sent to the thread by the
clr
I don't think I said anything that contradicts this.
if you use create thread as the example did, a new process thread is
created, a thread is not taken from the clr thread pool. you need to
use clr thread pool api to do this.


I don't believe I said this either. I said:

"ASP.NET threads are not created and terminated for each request nor are
their lifetimes tied to the lifetime of the page (or handler). Threads are
are drawn from the CLR thread pool to execute requests in ASP.NET. Once the
request is done the thread is returned to the CLR thread pool. The thread
pool may, over time, create and destroy threads, but this is based upon load
upon the server. It is not tied to each request and/or page."

The context for this entire description was threads used by ASP.NET to process
requests, not manually created threads.

-Brock
DevelopMentor
http://staff.develop.com/ballen

Nov 19 '05 #7
Mea Culpa, Brock.

I was indeed confusing this with other environments. I'm afraid I'm not an
ASP.Net specialist, so I misspoke. A Thread spawned by an ASP.Net Page will
indeed outlast the Page thread if the Page thread terminates first.

--
Thanks for keeping me honest!

Kevin Spencer
Microsoft MVP
..Net Developer
Everybody picks their nose,
But some people are better at hiding it.

"Brock Allen" <ba****@NOSPAMd evelop.com> wrote in message
news:10******** *************** @msnews.microso ft.com...
I'm not sure where you got this information, except perhaps you're
confusing this with other environments.
The lifetime of a child thread is limited to the lifetime of the
parent thread.


This is not correct. If a parent thread is terminated it does not
implicitly affect a child thread.
As an ASP.Net Page instance lasts for several
milliseconds, your thread is dying almost immediately, when the Page
class is unloaded.


ASP.NET threads are not created and terminated for each request nor are
their lifetimes tied to the lifetime of the page (or handler). Threads are
are drawn from the CLR thread pool to execute requests in ASP.NET. Once
the request is done the thread is returned to the CLR thread pool. The
thread pool may, over time, create and destroy threads, but this is based
upon load upon the server. It is not tied to each request and/or page.

-Brock
DevelopMentor
http://staff.develop.com/ballen

Nov 19 '05 #8

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

Similar topics

0
1048
by: Phil | last post by:
I recently replaced my Toshiba 6100 laptop running XP Pro with a Dell Latitude D810 running XP Pro; since that time an application that I developed over a year ago has stopped working. I am using the same development environments, code, and third-party components. Nothing is different except the development computer. If I use my test app, written in C#, to "drive" the DLL, the thread starts and the program works flawlessly on the Dell. ...
23
6541
by: Jeff Rodriguez | last post by:
Here's what I want do: Have a main daemon which starts up several threads in a Boss-Queue structure. From those threads, I want them all to sit and watch a queue. Once an entry goes into the queue, grab it and run a system command. Now I want to make sure that system command doesn't hang forever, so I need some way to kill the command and have the worker thread go back to work waiting for another queue entry.
5
4028
by: Serge | last post by:
Hi, I am having a thread hang problem in my c# code. The example on the website: http://csharp.web1000.com/ is a simplified version of my problem. You will see in the form that a method TestThread increments a number in the textbox on the form. TestThread is called from a worker thread (2nd thread) using a TimerThread.
12
2326
by: Ricardo Pereira | last post by:
Hello all, I have a C# class (in this example, called A) that, in its constructor, starts a thread with a method of its own. That thread will be used to continuously check for one of its object's state and generate classe's A events "Connected" and "Disconnected". It looks something like this: "
7
439
by: Ivan | last post by:
Hi there My work on threads continues with more or less success. Here is what I'm trying to do: Class JobAgent is incharged for some tasks and when it's called it starts thread which performs the job. Application contains one list of agents that are idle at the moment and list of busy agents. In loop it checks if there are agents in idle list and if there are some, it starts them.
5
1629
by: Doug Kent | last post by:
Hi, I am using a STA thread to run a COM object. On a couple of machines the thread runs fine. On another machine the thread won't start, and no exceptions are thrown. This code is running in a web service implemented using C#, ASP.NET 1.1, IIS 5.1, Windows 2000 Server.
8
1624
by: Jason Chu | last post by:
I have a webpage which uploads a big file onto access db. if the file is say around 30 megs, it'll take around a minute for it to get put into the access db. I didn't want the user to wait for it, so I decided to put it on a thread. The thread works, but not the way it should. One of my page will start the thread, and then forward the user to another page. The thread will start, and browser will get forwarded to the next page, BUT as...
5
2220
by: taylorjonl | last post by:
I am completely baffled. I am writting a daemon application for my work to save me some time. The application works fine at my home but won't work right here at work. Basically I have a MainForm what has a Start/Stop button that starts and stops the processing thread. private void StartButton_Click(object sender, System.EventArgs e) { if( bStopSignal ) { // disable controls that aren't valid when running
0
1084
by: Nick | last post by:
hi, I have a asp.net project. I want to start a thread in this project at some point to do some operations on database.Is there a limitation on this thread about how long it could run? Is it possible to make it run all the time to do monitor job? For me, looks like it works on my machine. But when I put it to the my webhost, public it, it won't work. It did not run into the thread at all. And It did not say any error about new thread...
0
1527
by: Yue Fei | last post by:
I have a multi thread python code, threads can start immediately if I run on command line, but I can get them started right the way if I call the same code from C/C++. test code like this: from threading import Thread import thread class testThread(Thread): def __init__ (self, id): Thread.__init__(self) self.id = id def run(self):
0
8946
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
8774
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9447
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
9181
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...
1
6735
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4550
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
4809
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3261
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
3
2180
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.