c# - Async await button click always running synchronously -


i learning async await , implementing in old asp.net. using c# 4.6.

now page running synchronously after adding async-await. it's waiting api send result , shows message on screen.

what looking page kicks off thread , responsive(i can other bits on it). when it's completed, shows result.

below button click, http call (which initiate async req) , api method .

i have

  1. followed examples https://msdn.microsoft.com/en-us/library/hh191443.aspx
  2. looked solution on stackoverflow , other forums. believe doing same way (which of course not :( ).

added async="true" on aspx page.

button click

protected async void btncopy_click(object sender, eventargs e)     {         await runasync(guid.parse(sourcebusinessid), guid.parse(destinationbusinessid), false);          if (lblerror.text == "")         {             lblerror.text = "copy completed!";         }                 }  public async task runasync( guid sourcebusinessid, guid destinationbusinessid,bool copyadviser)     {         using (var client = new httpclient())         {             var request = new httprequestmessage()             {                 requesturi = new uri("http://localhost:52140/api/dummyaccounts/"),                 method = httpmethod.get,             };             request.headers.accept.add(new mediatypewithqualityheadervalue("text/plain"));              client.timeout = timespan.fromseconds(500);              var response = await client.sendasync(request);              if (!response.issuccessstatuscode)             {                                     lblerror.text = response.statuscode.tostring();             }         }     } 

api

public ihttpactionresult get(guid sourcebusinessid, guid destinationbusinessid,bool copyadviser = true)     {         try         {             copyhelper.sourcebusinessid = sourcebusinessid;             copyhelper.destinationbusinessid = destinationbusinessid;             copyhelper.copyadviser = copyadviser;              logger.info("copy buisness started:" + datetime.now);              bool status = copybusinessservice.copybusiness(copyhelper.sourcebusinessid, copyhelper.destinationbusinessid, copyhelper.copyadviser);             if (status)             {                 logger.info("copy business finished:" + datetime.now);                  return ok(true);             }             else             {                 logger.info("copy business failed:" + datetime.now);                  return ok(false);             }         }         catch (exception ex)         {             return internalservererror(ex);         }      } 

any appreciated.

async in webforms application doesn't make form behave client side application sits , in event loop , waits events happen. it's still server rendered page responds single, transactional http requests triggered button click or change event, single server rendered html response webform. once rendered page done. there's no 'event loop' waits next click.

async in webforms/asp.net app allows offload processing main thread, doesn't not change behavior of ui on page. think of async way either run multiple operations simultaneously, or release main processing thread while longer running io operation runs in background, returns before final page rendered.

but doesn't way page render browser or behave - webforms page still purely server rendered page handles events via http postbacks. far browser concerned it's transactional request: click button , page re-renders regardless of whether use async on backend or not.

the way solve problem use client side js framework manage application's async flow , make asynch http calls server. give behavior looking for, long request can run 'in background' while letting client side application continue other operations. server calls when operation complete , can use response data display results or notify user.


Comments

Popular posts from this blog

Ansible - ERROR! the field 'hosts' is required but was not set -

customize file_field button ruby on rails -

SoapUI on windows 10 - high DPI/4K scaling issue -