Some time, i posted on MSDN forum asking for a help, and people in there are told me to ask for my question here because they told me i need to solve my problem with AJAX, but have no idea how, thats was my question on MSDN:
Hi,
im trying to understand better async and sync, for that i need some help.
I create a simple project(windows form) with 2 buttons(sync, async), the code below:
private void button1_Click(object sender, EventArgs e)
{
Thread.Sleep(2000);
label1.Text = "Task Completed";
}
private async void button2_Click(object sender, EventArgs e)
{
label2.Text = await ProcessDataAsync1();
label4.Text = await ProcessDataAsync2();
label5.Text = await ProcessDataAsync3();
}
public Task<string> ProcessDataAsync1()
{
return TaskEx.Run(() =>
{
Thread.Sleep(2000);
return "Task 1 Completed";
});
}
Using this code, works like i want, if i click on the sync button, my form doest not allow me to do anything until the request finish, and if i click on the async button, this request will fill my labels one by one, even if i click on the sync during this request, the async request will continue filling my labels.
So my question, on webforms, the behavior is different, if i use the same code, the request will fill my labels together and only at the END of request.
How should i work to have on my webForm the same behavior that i have on my windowsform?
thankz in advance!
when i ask that o msnd , i recevei this as answer:
Hi Norkk,
In the world of WinForms and WebForms async updates to the UI are handled in very different ways. The reason that you see the behavior you do is because the async processes run asynchronously on the server side, but the server waits for them to finish before
sending the finished HTML to the client. To achieve similar partial page updates you'll need to use AJAX (Asynchronous JavaScript and XML). In this case you could use a combination of UpdatePanel (for partial page updates) and a Timer to force additional Postbacks
for each label.
I'm not sure if I can explain clearly since that might require many cross knowledge between winform, webform, async and ajax here.
first, the original code (you use for demonstration in winform) which is not applicable in webform. As you've got answer from MSDN forum, webform(ASP.NET page) has different underlying UI generation and threading model.
If time allowed, strongly recommend you get more ideas about how ASP.NET page processing pipeline and lifecycle work:
Then, about the AJAX approach. I think we defintely need to know what AJAX provides and its strength(unfortunately, it's better to understand the above ASP.NET server-side model first so that we can better understand why we should use AJAX).
In short, if you do not use AJAX and put all the code normally in ASP.NET webform's button_click event, the page will postback and after the work in button_click finished, write response back to client. What the AJAX do is make the postback in background. So
your pack does not postback, but send a background javascript request to server(like a webservice call or WCF service call) to fetch data. After the response come back(AJAX client use async model for such request calling) you use the retrieved data to update
UI.
Personally, we suggest that you can use JQuery for performing AJAX call in client-side javascript of web page:
norkk
Member
29 Points
30 Posts
Sync and Async.
Feb 28, 2012 12:36 PM|LINK
Some time, i posted on MSDN forum asking for a help, and people in there are told me to ask for my question here because they told me i need to solve my problem with AJAX, but have no idea how, thats was my question on MSDN:
Hi, im trying to understand better async and sync, for that i need some help. I create a simple project(windows form) with 2 buttons(sync, async), the code below: private void button1_Click(object sender, EventArgs e) { Thread.Sleep(2000); label1.Text = "Task Completed"; } private async void button2_Click(object sender, EventArgs e) { label2.Text = await ProcessDataAsync1(); label4.Text = await ProcessDataAsync2(); label5.Text = await ProcessDataAsync3(); } public Task<string> ProcessDataAsync1() { return TaskEx.Run(() => { Thread.Sleep(2000); return "Task 1 Completed"; }); } Using this code, works like i want, if i click on the sync button, my form doest not allow me to do anything until the request finish, and if i click on the async button, this request will fill my labels one by one, even if i click on the sync during this request, the async request will continue filling my labels. So my question, on webforms, the behavior is different, if i use the same code, the request will fill my labels together and only at the END of request. How should i work to have on my webForm the same behavior that i have on my windowsform? thankz in advance!norkk
Member
29 Points
30 Posts
Re: Sync and Async.
Feb 28, 2012 01:09 PM|LINK
when i ask that o msnd , i recevei this as answer:
Hi Norkk,
In the world of WinForms and WebForms async updates to the UI are handled in very different ways. The reason that you see the behavior you do is because the async processes run asynchronously on the server side, but the server waits for them to finish before sending the finished HTML to the client. To achieve similar partial page updates you'll need to use AJAX (Asynchronous JavaScript and XML). In this case you could use a combination of UpdatePanel (for partial page updates) and a Timer to force additional Postbacks for each label.
norkk
Member
29 Points
30 Posts
Re: Sync and Async.
Feb 28, 2012 01:10 PM|LINK
A1ien51
All-Star
29935 Points
5821 Posts
Re: Sync and Async.
Feb 28, 2012 02:20 PM|LINK
Have you done any research on AJAX?
That would be my first step.
:)
norkk
Member
29 Points
30 Posts
Re: Sync and Async.
Feb 28, 2012 04:14 PM|LINK
All people who answer told me to use AJAX, but i search a lot, and nothing that im found helps me.
you know how can solve this problem?
raju dasa
Star
14412 Points
2452 Posts
Re: Sync and Async.
Feb 28, 2012 04:26 PM|LINK
HI,
check this site: for basic AJAX and details.
http://www.w3schools.com/ajax/default.asp
rajudasa.blogspot.com || blog@opera
norkk
Member
29 Points
30 Posts
Re: Sync and Async.
Feb 28, 2012 04:45 PM|LINK
i dont have any doubts about AJAX, just dont have any ideas, to how solve this problem. thats the reason why i ask here.
norkk
Member
29 Points
30 Posts
Re: Sync and Async.
Feb 29, 2012 10:56 AM|LINK
someone here, knows how could i solve that, using AJAX?
thnkz !
Steven Cheng...
Contributor
4199 Points
548 Posts
Microsoft
Moderator
Re: Sync and Async.
Mar 01, 2012 06:26 AM|LINK
Hi Norkk,
I'm not sure if I can explain clearly since that might require many cross knowledge between winform, webform, async and ajax here.
first, the original code (you use for demonstration in winform) which is not applicable in webform. As you've got answer from MSDN forum, webform(ASP.NET page) has different underlying UI generation and threading model.
If time allowed, strongly recommend you get more ideas about how ASP.NET page processing pipeline and lifecycle work:
#A low-level Look at the ASP.NET Architecture
http://www.west-wind.com/presentations/howaspnetworks/howaspnetworks.asp
#ASP.NET Page Life Cycle Overview
http://msdn.microsoft.com/en-us/library/ms178472.aspx
Then, about the AJAX approach. I think we defintely need to know what AJAX provides and its strength(unfortunately, it's better to understand the above ASP.NET server-side model first so that we can better understand why we should use AJAX).
In short, if you do not use AJAX and put all the code normally in ASP.NET webform's button_click event, the page will postback and after the work in button_click finished, write response back to client. What the AJAX do is make the postback in background. So your pack does not postback, but send a background javascript request to server(like a webservice call or WCF service call) to fetch data. After the response come back(AJAX client use async model for such request calling) you use the retrieved data to update UI.
Personally, we suggest that you can use JQuery for performing AJAX call in client-side javascript of web page:
#Easy Ajax with jQuery Article
http://www.sitepoint.com/ajax-jquery/
#Using jQuery for AJAX in ASP.NET
http://www.codeproject.com/Articles/17203/Using-jQuery-for-AJAX-in-ASP-NET
#Using jQuery with ASP.NET - A Beginner's Guide
http://www.dotnetcurry.com/ShowArticle.aspx?ID=231
#Using jQuery with ASP .NET
http://dotnetslackers.com/articles/ajax/using-jquery-with-asp-net.aspx
Feedback to us
Microsoft One Code Framework
shafkatlee
Member
152 Points
53 Posts
Re: Sync and Async.
Mar 02, 2012 05:19 AM|LINK
Hi,
You should try AJAX.
AJAX is Async technology foe WEb.