11

My Webforms application which is based on .Net 4.6 has to use the async/await-functionality quite extensively. Because I'm quite new to this async/await topic I read quite a lot of best practices like this or this. But I still have some questions for which I haven't found any clear informations.

  1. Regarding Page-Lifecycle-Events: I know that e.g. for the Page_Load-Event it's best practice to avoid async void-methods and register such methods like this:

    protected void Page_Load(object sender, EventArgs e)
    {
     PageAsyncTask pageAsyncTask = new PageAsyncTask(SomeAsyncMethod);
     Page.RegisterAsyncTask(pageAsyncTask);
     //Method to invoke the registered async-methods immedietly and not after the PreRender-Event
     Page.ExecuteRegisteredAsyncTasks();
     }
    

    My problem is that I want to call the async-method as soon as I registered it and not after the OnPreRender-event. This should be achieved by calling the ExecuteRegisteredAsyncTasks()-method. But in my case this has no effect and the async-method is still invoked after the PreRender-event. But why?

  2. Regarding Control-Events: Is it better to register async-methods the same way I mentioned in the code-example above or can I use the async-void signature like:

    protected async void OnClick(object sender, EventArgs e)
    {
    await SomeAsyncMethod();
    }
    

    I found both examples but no clear informations which is the better solution and why.

  3. Regarding Context and ConfigureAwait, it seems to be best practice to use await SomeAsyncMethod.ConfigureAwait(false) for a better performance and where the context is not important and not to use it where the context e.g. when manipulating GUI elements. But in my case it seems to make no difference if I call await SomeAsyncMethod.ConfigureAwait(false) in my click-event. I can still manipulate my GUI-elements wihtout any problems. The example which I uses was this:

    private async void button1_Click(object sender, EventArgs e)
    {
     button1.Enabled = false;
     try
     {
     await SomeAsyncMethod().ConfigureAwait(false);
     }
     finally
     {
     //Manipulating still works even it's another context
     button1.Enabled = true;
     }
    }
    

So I wonder why the manipulating of the GUI-elements still work and if I really should use ConfigureAwait(false) on every async-method where the context is not important, which is quite tedious. I wonder if this has something to do with the usage of the Ajax-Functionality by Telerik which I use for my Webapplication. But this is just an assumption.

halfer
20.1k19 gold badges110 silver badges207 bronze badges
asked Jun 6, 2017 at 8:45
1
  • 4
    There is no GUI in an ASP.NET application. WebForms gives you a model that looks like a GUI, but it is not. That said, I do not call ConfigureAwait(false) when that method has to call into ASP.NET APIs (such as button1.Enabled). Most APIs work just fine but there are a few that fail, so I just play it safe. Commented Jun 6, 2017 at 12:15

2 Answers 2

6

ASP.NET WebForms has it's own asynchronous execution engine. Please refer to the documentation.

In any case, you typically want (or need) to get back to the current synchronization context on methods such as event handlers, so you shouldn't call ConfigureAwait(false) inside button1_Click, but you should call it inside SomeAsyncMethod.

Lee Taylor
7,99416 gold badges38 silver badges53 bronze badges
answered Jun 6, 2017 at 11:03

3 Comments

Have already read that which I mentioned in my inital post. But I need more detailled informations especially why Page.ExecuteRegisteredAsyncTasks() is not working properly
What is SomeAsyncMethod() doing and what do you think Page.ExecuteRegisteredAsyncTasks() working properly is? I still prefer to register all asynchronous work and let it execute automatically a grab the values before render.
E.g. SomeAsyncMethod() loads some data at the pageload-event. This data is necessary e.g. inside a button-click event. The problem is that the async-method is executed after the prerender-event which is to late for the click-event. Because of that I thought I could use Page.ExecuteRegisteredAsyncTasks(). But calling this method has no effect and SomeAsyncMethod() is still called after the prerender-event
-4

My simple method for Asp.net 4.5 webforms:

1) declare aspx page with this attribute

<%@ Page ..... Async="true" ValidateRequest="false" EnableEventValidation="false" %>

2) Create this void method in code:

void MyAsyncMethod( ... list parameters )
{
 //Insert this on end method code
 var objThread = Session["MyAsyncMethod"] as Thread;
 if (objThread != null) objThread.Abort();
}

3) Call method MyAsyncMethod:

var objThread = new Thread(
 () => MyAsyncMethod(parameters..)) {IsBackground = true};
objThread.Start();
Session["MyAsyncMethod"] = objThread;
Fuzzybear
1,4212 gold badges28 silver badges44 bronze badges
answered Jun 29, 2018 at 14:55

1 Comment

Threads are expensive (they cause a 1MB stack allocation) - if you're going to use a Thread then use the ThreadPool (and Task.Run automatically uses the thread-pool). But your code doesn't asynchronously wait (await) for the thread or task to complete - also, NEVER EVER EVER call Thread.Abort()!

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.