Threading in asp.net issue - Thread stops

P

Phil

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
 
B

bruce barker

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)


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

John Saunders

Phil said:
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...
 
P

Phil

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,821
Latest member
AleidaSchi

Latest Threads

Top