locked
Difference between 'async private void Button_Click' and 'private async void Button_Click'

    Question

  • Hi Everybody,

    I made a windows phone 8.1 research project that showed some behavior what I could not explain, I made a async eventhandler for a button click, because the functions I had to use are awaitables so in order to await them I needed to define the eventhandler as async.

    I used the following sample code:

    captureMedia sample code

    First Code:

    This code completely crashes the windows phone at random (Nokia Lumia 520) Freezes, Debugging sessions crashes, Photo not taken, and sometimes it worked... 

    Unstable code:

    private async void btnCamera_Click(object sender, RoutedEventArgs e)
            {
                try
                {
                    if (this.cameraStarted == false)
                    {
                        this.imgPhoto.Visibility = Visibility.Collapsed;
                        this.cameraPreview.Visibility = Visibility.Visible;
    
                        if(this.captureManager == null)
                        {
                            this.captureManager = new MediaCapture();
                            await this.captureManager.InitializeAsync();
                        }
    
                        this.cameraPreview.Source = this.captureManager;
    
                        await this.captureManager.StartPreviewAsync(); //start camera
                        this.btnCamera.Content = "Take Photo";
    
                        this.cameraStarted = true;
                    }
                    else
                    {
                        ImageEncodingProperties imgFormat = ImageEncodingProperties.CreateJpeg();
    
                        StorageFolder localFolder = await (await Windows.Storage.ApplicationData.Current.LocalFolder.CreateFolderAsync("Test", CreationCollisionOption.OpenIfExists)).CreateFolderAsync("Camera", CreationCollisionOption.OpenIfExists);
                        
    ...
                        await captureManager.CapturePhotoToStorageFileAsync(imgFormat, file); // Take photo
    
                        await this.captureManager.StopPreviewAsync(); //stop streaming
    ...
     
                    }
                }
                catch (Exception exception)
                {
                    ...
                }
            }

    After 1,5 day of struggling to find out what was going on I noticed that the original sample code defined this eventhandler methods as:

    async private void btnCamera_Click

    instead of:

    private async void btnCamera_Click

    So i changed my code to:

    Stable code:

    async private void btnCamera_Click(object sender, RoutedEventArgs e)
            {
                try
                {
                    if (this.cameraStarted == false)
                    {
                        this.imgPhoto.Visibility = Visibility.Collapsed;
                        this.cameraPreview.Visibility = Visibility.Visible;
    
                        if(this.captureManager == null)
                        {
                            this.captureManager = new MediaCapture();
                            await this.captureManager.InitializeAsync();
                        }
    
                        this.cameraPreview.Source = this.captureManager;
    
                        await this.captureManager.StartPreviewAsync(); //start camera
                        this.btnCamera.Content = "Take Photo";
    
                        this.cameraStarted = true;
                    }
                    else
                    {
                        ImageEncodingProperties imgFormat = ImageEncodingProperties.CreateJpeg();
    
                        StorageFolder localFolder = await (await Windows.Storage.ApplicationData.Current.LocalFolder.CreateFolderAsync("Test", CreationCollisionOption.OpenIfExists)).CreateFolderAsync("Camera", CreationCollisionOption.OpenIfExists);
                        
    ...
                        await captureManager.CapturePhotoToStorageFileAsync(imgFormat, file); // Take photo
    
                        await this.captureManager.StopPreviewAsync(); //stop streaming
    ...
     
                    }
                }
                catch (Exception exception)
                {
                    ...
                }
            }

    Result:

    The stable code works like a charm! no problems at all !! :)

    I believe that there must be a logical explanation for this, I looked it up in the Internet but I could not find any related documentation.

    So I ask you ;)

    Question:

    What is the difference between the two method declarations? And how does this slightly correction means the difference between crashing a device and a properly working device?

    async private void btnCamera_Click

    and

    private async void btnCamera_Click

    Greeting,

    JelteV.



    • Edited by JelteV Friday, May 2, 2014 9:45 AM
    Friday, May 2, 2014 9:17 AM

Answers

  • there shouldn't be a difference in that. but maybe you having a timing issue. since its async code the runtime flow can be different

    Microsoft Certified Solutions Developer - Windows Store Apps Using C#

    Friday, May 2, 2014 11:12 AM