So I have a button and four labels in my .aspx page. When I click the button I want the labels to be assigned text after sleeping for a few seconds each. However, I want them to post back on the page as they finish.
Ie. Label 3 takes 3 seconds to complete, Label 1 takes 5 seconds to complete, Label 2 takes 7 seconds to complete, Label 4 takes 12 seconds to complete.
So in the webpage, we see Label 3 appear, then Label 1 etc. Not all three coming back at the same time.
.cs
public string ProvincesAndSatesRetunServiceMethod()
{
int min = 1000;
int max = 10000;
var rand = new Random();
Stopwatch time = new Stopwatch();
time.Start();
System.Threading.Thread.Sleep(rand.Next(min, max));
time.Stop();
return " returned from method in " + time.Elapsed.Seconds + "seconds";
}
protected void PostAll_Click(object sender, EventArgs e)
{
Label1.Text = ProvincesAndSatesRetunServiceMethod();
Label2.Text = ProvincesAndSatesRetunServiceMethod();
Label3.Text = "No Responese Received";
Label4.Text = ProvincesAndSatesRetunServiceMethod();
}
.aspx
<%--IRE FAKING SERVICE --%>
<asp:UpdatePanel ID="testUpdatePanel" runat="server">
<ContentTemplate>
<asp:Button ID="btnAllPosts" runat="server" Text="Post All" onclick="PostAll_Click" autopostback="false" />
<asp:Button ID="btnReset" runat="server" Text="Reset" onclick="btnReset_Click" /><br />
<asp:Label ID="Label1" runat="server" /><br />
<asp:Label ID="Label2" runat="server" /><br />
<asp:Label ID="Label3" runat="server" /><br />
<asp:Label ID="Label4" runat="server" /><br />
</ContentTemplate>
</asp:UpdatePanel>
Right now all labels post back at the same time on the page after all methods are finished running. I need this functionality in real life and in that case a servvice is called from several different networks and each network posts back information after they have finished computations. It is important to show these as they arrive since users are waiting for them to be finished and some can take up to five minutes.
This service is out of my control and cannot be changed.
Thank you in advance for any help! Sorry for the newb mistakes 🙂
EDIT: I have now been able to have this work in a WCF web service that is async. It works very well but in the AsyncCompleted method, it posts back after all threads have completed in parallel. I need them to appear inside the update panel on the .aspx page as the threads are finished. Any ideas on how to do this?
Thanks in advance.
If it’s not targeting tech-savvy users, you may accomplish this task simply by delaying on the client side instead of waiting (and consuming resources) on server-side. You may use simple javascript setTimeout or jQuery delay functions. Have a look, it may make your job much easier if that’s what you want — a fake waiting. But note that who is good with computers/knowing programming will notice what you are doing.