0

I am trying to accomplish a task which I am stuck at and need your help on.

I have made an Web Form website in Visual Studio 2015.

I have a BUTTON on a page and a DIV which shows result from web requests.

I am calling GetAsync on three url and making 3 web requests. Lets name them Request1, Request2 and Request3.

Now let's suppose, Request1 takes 1 seconds, Request2 takes 5seconds and Request3 takes 10 seconds to get data.

What happens is that the website completes all async task first and then shows the data.

I want data to be showed as first downloaded first show basis.

private async Task PerformSearchAsync()
    {

        HttpClient client = new HttpClient();
        List<string> urlList = SetUpURLList();

        IEnumerable<Task<RootObject>> downloadTasksQuery =
            from url in urlList select ProcessURL(url, client);

        List<Task<RootObject>> downloadTasks = downloadTasksQuery.ToList();

        while (downloadTasks.Count > 0)
        {
            // Identify the first task that completes.
            Task<RootObject> firstFinishedTask = await Task.WhenAny(downloadTasks);

            // ***Remove the selected task from the list so that you don't
            // process it more than once.
            downloadTasks.Remove(firstFinishedTask);

            // Await the completed task.
            RootObject rootObject = await firstFinishedTask;
            DisplayResult(rootObject);
        }


    }

private void DisplayResult(RootObject rootObject)
    {
        string s = "Name: "+rootObject.name+ " <span><img src =\"http://"+rootObject.logo+"\" alt=\"Source Logo\" style=\"width:16px;height:16px;\"></span>";
        Source.InnerHtml += s;
    }

So what I want to do is if Request1 is completed show its result on web page immediately and then when Request2 or Request3 is completed update the webpage with that request's result

Thank you in advance

Raunaq Patel
  • 270
  • 2
  • 12
  • 1
    Then you need make three different request from the client. – Fabio Jul 16 '16 at 11:25
  • @Fabio these are three different calls – Raunaq Patel Jul 16 '16 at 11:38
  • With your code first call to the `PerformSearchAsync` will return response only after all downloadTask will be completed. II meant that you create one method which "perform" only one search task. Then call that method from your client (ajax call) separatly with three different parameters – Fabio Jul 16 '16 at 11:42
  • @Fabio I do not have much experience with Ajax calls. Cam you point me in right direction with some tutorial or reference? That would be a great help. Thank you. – Raunaq Patel Jul 16 '16 at 14:40
  • [http://stackoverflow.com/questions/202538/using-jquery-for-ajax-with-asp-net-webforms](http://stackoverflow.com/questions/202538/using-jquery-for-ajax-with-asp-net-webforms) – Fabio Jul 16 '16 at 14:46

1 Answers1

0

Task.WhenAny(downloadTasks) run different in windows GUI (WinForm) than WebForm.

In WebForm - ASP.NET knows to keep the request alive until all asynchronous operations for that request have completed.

Async Doesn't Change the HTTP Protocol.

The async operations work in the context of asp.net on the server not in the client(browser), so :

What happens is that the website completes all async task first and then shows the data :) (as you said)

if you enabled Trace, you can get a result like the following (for downloading 5 links) to know what happen:



   CategoryMessageFrom First(s)From Last(s)
 aspx.pageBegin PreInit  
 aspx.pageEnd PreInit0.0001290.000129
 aspx.pageBegin Init0.0001430.000014
 aspx.pageEnd Init0.0001680.000024
 aspx.pageBegin InitComplete0.0001790.000011
 aspx.pageEnd InitComplete0.0001910.000012
 aspx.pageBegin LoadState0.0002050.000014
 aspx.pageEnd LoadState0.0007550.000550
 aspx.pageBegin ProcessPostData0.0007720.000017
 aspx.pageEnd ProcessPostData0.0012780.000506
 aspx.pageBegin PreLoad0.0012950.000017
 aspx.pageEnd PreLoad0.0013110.000016
 aspx.pageBegin Load0.0013240.000013
 aspx.pageEnd Load0.0013530.000029
 aspx.pageBegin ProcessPostData Second Try0.0013650.000012
 aspx.pageEnd ProcessPostData Second Try0.0013750.000010
 aspx.pageBegin Raise ChangedEvents0.0013860.000011
 aspx.pageEnd Raise ChangedEvents0.0014000.000014
 aspx.pageBegin Raise PostBackEvent0.0014100.000011
 aspx.pageEnd Raise PostBackEvent0.0021300.000720
 aspx.pageBegin LoadComplete0.0021500.000019
 aspx.pageEnd LoadComplete0.0021620.000012
 aspx.pageBegin PreRender0.0021730.000011
 aspx.pageEnd PreRender0.0021930.000021
  http://msdn.microsoft.com0.0301360.027942
  http://msdn.microsoft.com/library/windows/apps/br211380.aspx4.5581914.528055
  http://msdn.microsoft.com/en-us/library/hh290136.aspx18.06925713.511066
  http://msdn.microsoft.com/en-us/library/dd470362.aspx23.2873405.218083
  http://msdn.microsoft.com/en-us/library/aa578028.aspx25.5387092.251369
  http://msdn.microsoft.com/en-us/library/ms404677.aspx30.5350954.996386
  http://msdn.microsoft.com/en-us/library/ff730837.aspx36.9596576.424562
 aspx.pageBegin PreRenderComplete41.3143724.354715
 aspx.pageEnd PreRenderComplete41.3148800.000508
 aspx.pageBegin SaveState41.3152210.000341
 aspx.pageEnd SaveState41.3171340.001914
 aspx.pageBegin SaveStateComplete41.3171850.000051
 aspx.pageEnd SaveStateComplete41.3172100.000025
 aspx.pageBegin Render41.3172340.000025
 aspx.pageEnd Render41.3193730.002138


So the page will be rendered after Complete execution of all tasks, as you see in the trace result above.

I suggest, as commented by Fabio, use Jquery ajax call separatly with different parameters.

you can read: AsyncTask with a Updatepanel?

Community
  • 1
  • 1
M.Hassan
  • 10,282
  • 5
  • 65
  • 84