473,569 Members | 2,844 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(thi s.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(10 0);
}
}

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 1453
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(thi s.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.goo gle.com...
| 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(thi s.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(10 0);
| }
| }
|
| 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.goo gle.com...
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(thi s.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(10 0);
}
}

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******* *******@TK2MSFT NGP14.phx.gbl>. ..
"Phil" <ph************ @hotmail.com> wrote in message
news:78******** *************** ***@posting.goo gle.com...
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(thi s.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(10 0);
}
}

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
5224
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 the moment. I'd be *very* grateful if people with any interest in multi-threading would read it (even just bits of it - it's somewhat long to go...
15
2379
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 Thread(new ThreadStart(this.Import)); t.Priority = ThreadPriority.Lowest;
13
1799
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. I know of the problems with the worker process doing two things at once from the main thread pool (The old "Sit in a tight loop for 20 seconds and...
11
2174
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 number of mail messages, reporting back to the UI thread through the use of a Queue object. When all messages that are expected have been received,...
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 hours, then freezes. I'm stuck. How do I debug this? (Using: Python 2.4.3, wxPython 2.6.3.2, Windows 2000 and XP) There are no tracebacks, the...
5
10880
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 have a form that has a button. ( this form is a child form of a parent form ( main form ). Anway...in this child form I have a button, and if clicked...
3
1443
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, the log files a big and it takes time to read down as it get further in to log said, a 100,000 line in I have changed it to mutli-threading which...
126
6649
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: http://www.cse.wustl.edu/~schmidt/ACE.html the same way that the STL (and subsequently BOOST) have been subsumed? Since it already runs on zillions of platforms, they have...
12
1393
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 ) won't give any item more than once. But it does !!! Again I wrote this code improperly to see what happens but I don't understand why it happens...
0
7612
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...
0
7922
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. ...
0
8119
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...
1
7668
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7964
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...
1
5509
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...
0
5218
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...
0
3653
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...
0
936
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...

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.