Reputation: 7144
My goal:
What I have (this works, but it's not quite what I want):
The code so far:
DatabaseChecker.aspx.cs
protected void btnStartProcess_Click(object sender, EventArgs e)
{
Session["Running"] = true;
Session["TextBoxContent"] = "Beginning myTimeConsumingProcess()...";
Thread thread = new Thread(new ThreadStart(MyTimeConsumingProcess));
thread.Start();
Response.Redirect("Processing.aspx");
}
private void myTimeConsumingProcess()
{
//do stuff!
Session["TextBoxContent"] += "SUCCESS!"
Session["Running"] = false;
}
Processing.aspx
Refresh the page every second:
<meta http-equiv="refresh" content="1" />
Processing.aspx.cs
Stay on this page, refreshing every 1 second, until the thread (left in DatabaseChecker.aspx.cs) turns the Session variable "Running" to false.
protected void Page_Load(object sender, EventArgs e)
{
TextBox1.Text = Session["TextBoxContent"].ToString();
Session["TextBoxContent"] += ".";
if ((bool)Session["Running"] == false)
{
Response.Redirect("DatabaseChecker.aspx");
}
}
What I want that I can't figure out:
If possible, I'd like to avoid redirecting to another page, and instead just update the TextBox in the page where the button was clicked. I tried just redirecting to the same page using a QueryString to indicate where I am in the process. However, this doesn't work --- the code to redirect to the same page IS reached, and the text IS updated, however nothing appears to happen UNTIL the process is complete (which defeats the purpose of the thread).
I have tried every alternative I can think of, including UpdatePanel
, Timer
PageAsyncTask
controls, and virtually every combination I could think of by using those controls in combination with Threads, and with the .aspx page's Async property set to true.
Upvotes: 3
Views: 2889
Reputation: 134
Maybe if you set the second parameter of Response.Redirect
to true
, so it does not wait the page processing.
public void Redirect(string url, bool endResponse);
Upvotes: 0
Reputation:
Have you tried using AJAX.
When they click the button triggering an AJAX function which starts your time consuming process.
Then use AJAX to poll the page for progress.
Added some code to explain how I have done this in the past.
I am not actually sure you even need an update panel for this but I have one.
This is some cut out pieces of code I have that does what I believe you want. The orginal code processed images, and displayed a progress bar to the user.
I have AjaxControlToolkit and Ajax as references in the project.
http://ajaxcontroltoolkit.codeplex.com/
This adds the script manager to the page
<ajaxToolkit:ToolkitScriptManager runat="server" ID="scriptManageer" EnablePartialRendering="true" AsyncPostBackTimeout="900" />
This is the "Important" part of the page.
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<div id="message"> </div>
<button id="btnSave" class="saveButton" onclick="doSomething();return false;">Save</button>
</ContentTemplate>
</asp:UpdatePanel>`
This javascript makes the polling work.
<script type="text/javascript">
function startPoll()
{
Default.DoThing();
myInterval = setInterval("poll()", 1000); //If you really want to poll that often
}
function poll()
{
progress = Default.ThingProcess();
if (progress == "100%")
{
clearInterval(myInterval);
}
document.getElemenyById("message").innerHTML = progress;
}
</script>
And this is my codebehind.
public partial class Default : System.Web.UI.Page
{
protected void Page_load(object sender, EventArgs e)
{
Ajax.Utility.RegisterTypeForAjax(typeof(Default));
}
[Ajax.AjaxMethod]
public void DoThing()
{
Thread thread = new Thread(new ThreadStart(MyTimeConsumingProcess));
thread.Start();
//The MyTimeConsumingProcess updates a var for thing progess
}
[Ajax.AjaxMethod]
private String ThingProgress()
{
//Reads some var
return progressNumber + "%"
}
}
Because of cut and pasting it may have things that aren't needed but shouldn't stop it from working.
Upvotes: 1