Sanny Pathak Sanny Pathak - 1 month ago 7
C# Question

The code before and after await is running twice

I have created one function to find whether SD card is present or not, But the code before and after var await is running twice. On button click i am calling the InvokeRefresh() function but the code before and after the line "var sdfiles = await removableDevices.GetFilesAsync();" is running twice.

<Page
x:Class="App7.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:App7"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">

<Grid>
<Button x:Name="button" Content="Click me" HorizontalAlignment="Left" Margin="144,261,0,0" VerticalAlignment="Top" Click="button_Click"
RenderTransformOrigin="-0.48,0.63"/>

</Grid>
</Page>



using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.Storage;
using Windows.UI.Popups;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;

// The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=234238

namespace App7
{
/// <summary>
/// An empty page that can be used on its own or navigated to within a Frame.
/// </summary>
public sealed partial class MainPage : Page
{
public MainPage()
{
this.InitializeComponent();

this.NavigationCacheMode = NavigationCacheMode.Required;
}

/// <summary>
/// Invoked when this page is about to be displayed in a Frame.
/// </summary>
/// <param name="e">Event data that describes how this page was reached.
/// This parameter is typically used to configure the page.</param>
protected override void OnNavigatedTo(NavigationEventArgs e)
{
// TODO: Prepare page for display here.

// TODO: If your application contains multiple pages, ensure that you are
// handling the hardware Back button by registering for the
// Windows.Phone.UI.Input.HardwareButtons.BackPressed event.
// If you are using the NavigationHelper provided by some templates,
// this event is handled for you.
}

private void button_Click(object sender, RoutedEventArgs e)
{
InvokeRefresh();
}

private async void InvokeRefresh()
{
StorageFolder removableDevices;
removableDevices = (await KnownFolders.RemovableDevices.GetFoldersAsync()).FirstOrDefault();
if (removableDevices == null)
{
MessageDialog mssgboxx1 = new MessageDialog("SD card not found. Add external SD card with downloaded bastas.");
await mssgboxx1.ShowAsync();
}
else
{
Debug.WriteLine("before");
var sdfiles = await removableDevices.GetFilesAsync();
var epath = await removableDevices.CreateFolderAsync("ebasta", CreationCollisionOption.OpenIfExists);
Debug.WriteLine("after");
}
}
}
}


OUTPUT:

before

before

after

after

Answer

First thing I would try is change this bit of code. If for whatever reason the button is clicked twice InvokeRefresh will run twice at the same time rather than awaiting.

private async void button_Click(object sender, RoutedEventArgs e)
    {
        await InvokeRefresh();
    }

You will also have to change the method slightly:

private async Task InvokeRefresh()

Output

enter image description here

Comments