Wrox Programmer Forums
Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 4.5.1 > ASP.NET 4.5.1 General Discussion
|
ASP.NET 4.5.1 General Discussion For ASP.NET 4.5.1 discussions not relating to a specific Wrox book
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 4.5.1 General Discussion section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old September 25th, 2015, 08:05 AM
Registered User
 
Join Date: Sep 2015
Posts: 7
Thanks: 3
Thanked 1 Time in 1 Post
Default Why use finally block to prevent reentrancy in async code ?

Hi,

I am trying to understand the microsoft documentation on how to handle reentrancy issues. One of the methods it recommends is to disable the button so the user can't start another async operation before the original one is finished. The code microsoft provides is below:

Code:
private async void StartButton_Click(object sender, RoutedEventArgs e)
{
    // This line is commented out to make the results clearer in the output.
    //ResultsTextBox.Text = "";

    // ***Disable the Start button until the downloads are complete. 
    StartButton.IsEnabled = false; 

    try
    {
        await AccessTheWebAsync();
    }
    catch (Exception)
    {
        ResultsTextBox.Text += "\r\nDownloads failed.";
    }
    // ***Enable the Start button in case you want to run the program again. 
    finally
    {
        StartButton.IsEnabled = true;
    }
}
My question is why do we have to put StartButton.IsEnabled = true in a finally block ? Can't we put it right after the await statement ? I thought await prevented further code from executing in an async method unil the task was completed.
The Following User Says Thank You to dars For This Useful Post:
 
Old October 6th, 2015, 04:56 PM
Registered User
 
Join Date: Oct 2015
Posts: 1
Thanks: 0
Thanked 1 Time in 1 Post
Default

I understand that if you put "StartButton.IsEnabled = true" above the await statement, in try block, occuring an exception the StartButton never will be enabled again.
In Finally block is the best practice, because independent of any exception occur, the StartButton will enabled for a new action.

Regards
The Following User Says Thank You to juliancorrea For This Useful Post:
dars (October 8th, 2015)
 
Old December 15th, 2015, 05:17 AM
Registered User
 
Join Date: Dec 2015
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default Hi

Use the following steps to prevent the reentrancy in async code :-

1. Recognize Reentrancy

2. Handling Reentrancy

Disable the Start Button

Cancel and Restart the Operation

Run Multiple Operations and Queue the Output

3. Reviewing and Running the Example App

Thanks!!





Similar Threads
Thread Thread Starter Forum Replies Last Post
DeBugging the Code Problem Resolved, Finally John S BOOK: Stephens' C# Programming with Visual Studio 2010 24-Hour Trainer 0 June 8th, 2013 01:06 PM
Re: Try/Finally to prevent connection pooling leak spinout ASP.NET 2.0 Basics 4 June 19th, 2007 08:48 AM
Jump to a code block tlwms BOOK: Professional Assembly Language 0 July 16th, 2006 10:31 AM
a block of html code keyvanjan ASP.NET 1.0 and 1.1 Basics 2 July 14th, 2006 12:48 AM
Apartment Threading and Reentrancy AgentSmith Pro VB 6 1 December 31st, 2004 07:27 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.