473,508 Members | 2,088 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Threading in asp.net issue - Thread stops

Hi,
I've a simple page with one button.
In the OnClick event, I want to create a thread that will perform a
long runninf task.
Here is a sample code

in the OnClick

Thread t = new Thread(new ThreadStart(this.Import));
t.Priority = ThreadPriority.Lowest;
t.Start();

Import Function

public void Import()
{
for (int i=0;i<100;i++)
{
Response.Write(i.ToString() + "<br>");
Thread.Sleep(100);
}
}

The issue is that my thread stop working really quickly. In this
example, the thread only display figures from 0 to 9... instead of
100...
Is there something special I should now about running threads in
asp.net?
Please advice.
Best regards
Philippe GRACA
Nov 18 '05 #1
3 1449
for the thread to be able to do Response.Write, your page need to wait for
the thread to complete as the thread cannot write to a nonexistent page.
try:

Thread t = new Thread(new ThreadStart(this.Import));
t.Priority = ThreadPriority.Lowest;
t.Start();
t.Join(); // don't return request until thread completes

-- bruce (sqlwork.com)
"Phil" <ph************@hotmail.com> wrote in message
news:78**************************@posting.google.c om...
| Hi,
| I've a simple page with one button.
| In the OnClick event, I want to create a thread that will perform a
| long runninf task.
| Here is a sample code
|
| in the OnClick
|
| Thread t = new Thread(new ThreadStart(this.Import));
| t.Priority = ThreadPriority.Lowest;
| t.Start();
|
| Import Function
|
| public void Import()
| {
| for (int i=0;i<100;i++)
| {
| Response.Write(i.ToString() + "<br>");
| Thread.Sleep(100);
| }
| }
|
| The issue is that my thread stop working really quickly. In this
| example, the thread only display figures from 0 to 9... instead of
| 100...
| Is there something special I should now about running threads in
| asp.net?
| Please advice.
| Best regards
| Philippe GRACA
Nov 18 '05 #2
"Phil" <ph************@hotmail.com> wrote in message
news:78**************************@posting.google.c om...
Hi,
I've a simple page with one button.
In the OnClick event, I want to create a thread that will perform a
long runninf task.
Here is a sample code

in the OnClick

Thread t = new Thread(new ThreadStart(this.Import));
t.Priority = ThreadPriority.Lowest;
t.Start();

Import Function

public void Import()
{
for (int i=0;i<100;i++)
{
Response.Write(i.ToString() + "<br>");
Thread.Sleep(100);
}
}

The issue is that my thread stop working really quickly. In this
example, the thread only display figures from 0 to 9... instead of
100...
Is there something special I should now about running threads in
asp.net?


Philippe, the basic thing to know about threads and ASP.NET is - don't use
threads with ASP.NET.

The only truly safe ways to use threads with ASP.NET involve the threads
being totally independant of the request which spawned them. Your thread is
obviously dependant on the request, as it want to output more HTML to the
response.

John Saunders

P.S. The basic reason is that:
1) the request comes in, creating some objects like Request, Response and
Page.
2) Your thread then starts running, writing to Response.
3) The request terminates, destroying and/or invalidating all of the objects
like Request, Response and Page.
4) Your thread continues as though Request, Response and Page were still
valid, which - they're not...
Nov 18 '05 #3
Thx,
I was almost sure this was a bad idea ;)
What I'm trying to do is to propose to our users via a web interface
the capability to load 5 or 6 really huge XML files( > 100 Mb each) in
the App database (doing XSL tranform and then bulk inserts to sql
server). This should be done on demand almost immediatly since they do
not want to wait for any kind of batch or whatever. Once those files
are loaded, they can then perform comparisons and a lot of other
tasks.
On our end, the import job take around 13 minutes to complete but on
the user side, they are experiencing TCP problems (the connection is
lost but the job is indeed still running!!) . I though about this
threading approach essentially thinking of having a running thread
separate from the ASP.NET one so that users won't loose the HTTP
connection. I agree this is the bad way to solve the problem and maybe
an aynchronous Web service will do the trick. What do you think of
that? Any ideas, comment, code to share?
Regards and nice we :)
Philippe GRACA
"John Saunders" <jo**************@hotmail.com> wrote in message news:<O3**************@TK2MSFTNGP14.phx.gbl>...
"Phil" <ph************@hotmail.com> wrote in message
news:78**************************@posting.google.c om...
Hi,
I've a simple page with one button.
In the OnClick event, I want to create a thread that will perform a
long runninf task.
Here is a sample code

in the OnClick

Thread t = new Thread(new ThreadStart(this.Import));
t.Priority = ThreadPriority.Lowest;
t.Start();

Import Function

public void Import()
{
for (int i=0;i<100;i++)
{
Response.Write(i.ToString() + "<br>");
Thread.Sleep(100);
}
}

The issue is that my thread stop working really quickly. In this
example, the thread only display figures from 0 to 9... instead of
100...
Is there something special I should now about running threads in
asp.net?


Philippe, the basic thing to know about threads and ASP.NET is - don't use
threads with ASP.NET.

The only truly safe ways to use threads with ASP.NET involve the threads
being totally independant of the request which spawned them. Your thread is
obviously dependant on the request, as it want to output more HTML to the
response.

John Saunders

P.S. The basic reason is that:
1) the request comes in, creating some objects like Request, Response and
Page.
2) Your thread then starts running, writing to Response.
3) The request terminates, destroying and/or invalidating all of the objects
like Request, Response and Page.
4) Your thread continues as though Request, Response and Page were still
valid, which - they're not...

Nov 18 '05 #4

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

Similar topics

77
5208
by: Jon Skeet [C# MVP] | last post by:
Please excuse the cross-post - I'm pretty sure I've had interest in the article on all the groups this is posted to. I've finally managed to finish my article on multi-threading - at least for...
15
2372
by: Phil | last post by:
Hi, I've a simple asp.net page with one button. In the OnClick event, I want to create a thread that will perform a long runninf task. Here is a sample code in the OnClick Thread t = new...
13
1787
by: John | last post by:
I've got some reasonably complex business logic in my C# code, in a class called by a ASP.NET page. This takes around 3-4 seconds to execute. It's not dependent on SQL calls or anything like that....
11
2160
by: Steve Smith | last post by:
I have written a winforms application that launches approximately 150 threads with Thread.ThreadStart() Each thread uses CDO 1.21 to logon to a different Exchange mailbox and send/receive a...
3
1924
by: Mark rainess | last post by:
The program displays images from a motion jpeg webcam. (Motion jpeg is a bastardization of multi-part mime.) <http://wp.netscape.com/assist/net_sites/pushpull.html> It runs perfectly for about 4...
5
10870
by: Miro | last post by:
I will try my best to ask this question correctly. I think in the end the code will make more sence of what I am trying to accomplish. I am just not sure of what to search for on the net. I...
3
1438
by: Barkingmadscot | last post by:
I am having problems with Multi-threading and write to Key Access Database I have a log file that and collecting infomation from getting the info is a problem or is writing it the database,...
126
6609
by: Dann Corbit | last post by:
Rather than create a new way of doing things: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2008/n2497.html why not just pick up ACE into the existing standard:...
12
1388
by: =?Utf-8?B?SmFyb2Q=?= | last post by:
Hi! I've done some code using threading just to check if writing code improperly can cause serious issues when using somehow safe data types. So in my understanding a stack ( collection type )...
0
7326
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,...
0
7385
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...
0
7498
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
5629
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,...
1
5053
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...
0
3195
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...
0
1558
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 ...
1
766
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
418
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...

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.