C# Windows App - getting blocked

Hi,

I have made a simple windows c# store app - that is trying to save a file but the threads are getting blocked. It calls the fnSave_Results_to_file but never comes back. So I have mucked up my Async call? :)

How can I adjust this to make it work properly.

Thanks,

Ward.

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
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;

using System.Threading.Tasks;
using Windows.Storage;

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

namespace FileTest1
{
    /// <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 async Task<string> fnSave_Results_to_file(string file_name)
        {
            string data;

            try
            {
                Windows.Storage.StorageFolder localFolder = Windows.Storage.ApplicationData.Current.LocalFolder;

                data = "test";

                StorageFolder storageFolder = Windows.Storage.ApplicationData.Current.LocalFolder;
                StorageFile newFile = await storageFolder.CreateFileAsync(file_name, CreationCollisionOption.ReplaceExisting);

                await Windows.Storage.FileIO.WriteTextAsync(newFile, data);

                return newFile.Path;
            }

            catch (Exception ex)
            {
                throw ex;
            }
        }
        public MainPage()
        {
            this.InitializeComponent();
        }

        private void button_Save_File_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                Task<string> t = fnSave_Results_to_file("test.txt"); // gets jammed
                t.Wait();

                string filename = t.Result;
            }

            catch (Exception ex)
            {
                throw ex;
            }
        }

        private void button_Exit_Click(object sender, RoutedEventArgs e)
        {
            Application.Current.Exit();
        }
    }
}

Open in new window

LVL 1
whorsfallAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

CuteBugCommented:
Do this

 
private async void button_Save_File_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                 string filename  = await  fnSave_Results_to_file("test.txt"); 
            }

            catch (Exception ex)
            {
                throw ex;
            }
        }

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.