Reputation: 2224
I'm running ASP.NET 4.0 WEBFORMS
This problem bugs me... I ahve read up on a couple of threads on SO aswell as MSDN, but I think I'm missing the final piece of this Async puzzle....
What I want to accomplish is to update a label asynchronously, as a test I have set a button and a label. Then I have a loop which counts from 0 - 9 everytime the loop hits a number equal to or higher than seven i want it to update the label with the number.
I have gotten this to work synchronously, but then the label will end up showing only the last number. I have setup all methods and connected the event, but it won't update even though I'm using beginInvoke and asyncresult.
At the moment it does not update at all, to much testing got me back to square one? I have managed to get it to update, I even trieds with system.threading.thread.sleep(3000), to see if it updated to quickly, but that didn't work at all?
I'm puzzled anyone got a hint on how to achieve this?
Here's my code
First my counter class
public class counter
{
public static event numberHandler FoundNumber;
public delegate void numberHandler(string position);
public void theAscendator()
{
for (int i = 0; i < 10; i++)
{
if (i >= 7)
{
string labelText = i.ToString();
if(FoundNumber != null)
{
FoundNumber.BeginInvoke(labelText, new AsyncCallback(this.SevenAndOver),null);
}
}
}
}
public void SevenAndOver(IAsyncResult ar)
{
FoundNumber.EndInvoke(ar);
}
}
Now my mainpage(index.aspx) the codebehind
public partial class index : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
private void index_FoundNumber(string position)
{
// Response.Write(position);
Label1.Text = position;
// throw new NotImplementedException();
}
protected void Button1_Click(object sender, EventArgs e)
{
counter counterClass = new counter();
counterClass.theAscendator();
counter.FoundNumber += new counter.numberHandler(index_FoundNumber);
}
}
Upvotes: 1
Views: 355
Reputation: 52420
My guess is that you're new to web, and that you're probably a desktop guy (wpf/winforms.) This kind of question is all to common for people like you making the transition from single-tier applications. You have to remember that despite the webforms model looking like winforms, it's nothing of the sort. It's a thin veil over a stateless request/response client/server model - i.e. HTTP.
The reason this doesn't work is simply because when the asynchronous counter class completes, the page request has already completed. There is no way for you to modify the html because it's already been sent back to the browser. Once the click handler completes, the page is "done." Server-side asynchronous code in asp.net essentially runs "headless." It's best used to do some work that no longer requires the client.
So, how would you do this? Well, it's not obvious at first but asynchronous web work is typically driven by the client, not the server. The client being the browser, and the technology being AJAX - Asynchronous JavaScript and XML. The idea is that the event is handled on the server, the page is returned and then javascript on the client will poll the server for new data without making a full postback of the page.
Now that I've said all that, there is in fact a way to emulate two way asynchronicity but without an understanding of the fundamentals, you may find it tough going. If you're interested, the technology is called SignalR (http://signalr.net/). This allows the server (asp.net C#) to call methods on the client (javascript), and vice versa.
So, my advice would be to first understand the HTTP model at a fundamental level, then start looking at abstractions like ASP.NET.
Upvotes: 1