Everything stops when a Thread is running on the server

G

Guest

Hello,

I have a button that starts up a Thread and runs it in the background.

While the Thread is running, all the Ajax controls, including the events
fired by the PageRequestManager, stop firing until the Thread exits. So, I
have no way of updating the page while this Thread is running, which was the
point of putting it on the Thread.

The Ajax timer stops firing. Nothing works. It's very frustrating.

Can someone please help me?

Thanks,

Amrit Kohli
 
J

John Saunders [MVP]

Amrit Kohli said:
Hello,

I have a button that starts up a Thread and runs it in the background.

While the Thread is running, all the Ajax controls, including the events
fired by the PageRequestManager, stop firing until the Thread exits. So,
I
have no way of updating the page while this Thread is running, which was
the
point of putting it on the Thread.

The Ajax timer stops firing. Nothing works. It's very frustrating.

Can someone please help me?

Are you using Session state? I've recently read that requests for the same
session are serialized.

BTW, I hope your background thread doesn't reference anything in the Page or
Request, or any other part of the HttpContext. Once the page unloads, all of
that data is invalid, even if your thread is still accessing it.
 
G

Guest

Aidy,

Thanks for the response.

Here's the ASPX code:

<ajax:ScriptManager id="ScriptManager1" runat="server"
EnablePartialRendering="true" EnablePageMethods="true"/>
<ajax:Timer ID="ajaxTimer" runat="server" Interval="1"
OnTick="ajaxTimer_OnTick" />
<ajax:UpdatePanel ID="upLoadTools" runat="server"
UpdateMode="Conditional">
<ContentTemplate>
<div class="progress">
<app:progressBar id="ImportProgressBar" height="20em" runat="server" />
</div>
<asp:Button ID="_btnLoadTools" runat="server" CssClass="formbutton"
Text="Load Tools" OnClick="_btnLoadTools_Click" ToolTip="Load Tools" />
</ContentTemplate>
<Triggers>
<ajax:AsyncPostBackTrigger ControlID="ajaxTimer" EventName="Tick" />
</Triggers>
</ajax:UpdatePanel>

And here's the code that starts the thread:

protected void _btnLoadTools_Click(object sender, EventArgs e) {
this.ajaxTimer.Enabled = true;
Thread t = new Thread((ThreadStart)delegate { CreateTools(); });
t.IsBackground = true;
t.Start();
}

And here's the code in the Tick event of the Timer:

protected void ajaxTimer_OnTick(object sender, EventArgs e) {
this.ImportProgressBar.Percentage = Percentage;
this.upLoadTools.Update();
}

The OnTick event dies after the Thread is started. However, I did figure
this out.

I put a conditional Sleep on the Thread that gives up the processor to the
ASPX worker process. The problem is that the Thread was basically hogging
the ASPX worker process and wouldn't give up any time to the client. With
some strategically placed Thread.Sleeps, I was able to get the progress bar
to update correctly.

What I didn't realize is that this isn't the same kind of multi-threading
enviornment that I'm familiar with. If you start a Thread in the ASPX worker
process' space, it basically takes over the whole process space. If you
don't put it to sleep, it'll rarely give up the time to the client.

Thanks for the help and I hope this helps others figure out this problem.

Amrit
 
G

Guest

John,

I was using the Session to pass values back to the Progress Bar. However, I
stopped doing that. I now use a static variable to do the same thing. There
are other alternatives like a hidden field, but I'm okay with the static
field.

My last post explains how I fixed this problem.

Thanks for the response.

Amrit
 

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,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top