BackgroundWorker and showing a panel.

L

Larry R

I am trying to set a panel (that holds a progress image) to be visible
when a long running process is happening. Sounds simple, right :) What
happens is the panel never becomes visible. The load and prerender
events occur, but it is never displayed.

In the super simple example, I use the "hide panel", then click the "Do
Process". The next thing I see is the "done". Why won't the panel
display?

Here is the code:
--------------------------------------
ASPX
--------------------------------------
<%@ Page Language="VB" Async="true" AutoEventWireup="false"
CodeFile="TestBGWorker.aspx.vb" Inherits="Tasks_TestBGWorker" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Button ID="btnHide" runat="server" Text="Hide Panel" /><br
/>
<asp:Button ID="Button1" runat="server" Text="Do Process" />
<asp:panel ID="Panel1" runat="server" Height="50px"
Width="125px">
<asp:Label ID="Label1" runat="server"
Text="Processing......"></asp:Label></asp:panel>

</div>
<asp:Label ID="Label2" runat="server"></asp:Label>
</form>
</body>
</html>

---------------------------------------------
Code Behind
---------------------------------------------
Imports System.Threading
Imports System.ComponentModel
Partial Class Tasks_TestBGWorker
Inherits System.Web.UI.Page
Private WithEvents worker As BackgroundWorker

Protected Sub Button1_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Button1.Click
worker = New BackgroundWorker()
worker.WorkerReportsProgress = True
Panel1.Visible = True
worker.RunWorkerAsync()
End Sub

Protected Sub worker_DoWork(ByVal sender As Object, ByVal e As
System.ComponentModel.DoWorkEventArgs) Handles worker.DoWork
System.Threading.Thread.Sleep(2000)
worker.ReportProgress(10)
System.Threading.Thread.Sleep(5000)
End Sub

Protected Sub worker_ProgressChanged(ByVal sender As Object, ByVal
e As System.ComponentModel.ProgressChangedEventArgs) Handles
worker.ProgressChanged
Panel1.Visible = True
Label1.Text = e.ProgressPercentage.ToString()

End Sub

Protected Sub worker_RunWorkerCompleted(ByVal sender As Object,
ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles
worker.RunWorkerCompleted
Panel1.Visible = False
Label2.Text = "done...."""
End Sub


Protected Sub btnHide_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles btnHide.Click
Panel1.Visible = False
End Sub

End Class
 
M

Marina Levit [MVP]

A web page is not like a windows form. The HTTP request is made, the server
processes it, sends down a bunch of HTML, and that is it. If the server is
still processing something on another thread, that is all well and good, but
that page instance is done, sent to the client, and rendered on the client.
The connection is closed, the server no longer has a way of contacting the
client. Much less somehow manipulating that HTML to now be something
different.

In a windows form, you have these objects in memory, and they are just there
and being manipulated.

A web form is just something that ends up generating HTML to send down to
the client. As soon as that happens, it knows nothing about any of its
clients, the Page object it used to generate the page is eligible for
garbage collection (so long as nothing else is referencing it). The next
time the client makes a request, from the point of view of the server, it's
a completely new request, it creates a brand new Page instance to process
this request - even if it is a postback.
 
L

Larry R

So, how does one achive this effect? The task appears simple, use a
background thread to do some process, but update a foreground control?
 
M

Marina Levit [MVP]

Actually, that is not a simple task - I would say that is one of the more
complicated tasks in terms of programming for the web.

If you google, you can find some solutions people have come up with. I
imagine they involve either refreshing the page periodically and seeing if
the server is done with the task yet, then redirecting to a 'done' page. Or
having an XMLHTTP periodically query the server, then manipulate the page's
elements via javascript.

Programming for the web is entirely different then in the windows world.
You have to keep in mind that your page and all the controls on it are only
there as long as it takes to generate the HTML output. Once that is done,
they are all gone, and so is your connection to the client browser.
 
L

Larry R

well, I was able to accomplish the task using the Atlas UpdateProgress
control. It does exactly what I needed ( show a message while allowing
the background task to complete).
 

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,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top