none
synchronious call of async method RRS feed

  • Question

  • Hello,

    the problem is:

    I have a method:

    async void DoSave() ...

    Now on closing of the app (can be in wpf on close event or in Android) I have for example:

     

    privatevoidMainWindow_Closed(objectsender, EventArgse)

    {

         await DoSave();

    }

    Because DoSave Returns immediately, before saving is ready, the application closes. But the data are not saved.

    There cannot be a discussion to save the data at an other time - it must be saved in my case when the application closes.

    So, I Need:

    void DoSave_Wrapper()

    {

         await DoSave();  //but here must be something, so that this wrapper-method returns after the DoSave is ready.

    }

    One remark to the async/await pattern: it would be fine, if and only if the programer can decide to make Things async or synchroniously - so we have, Microsoft means to be the teacher of all programers of the world.

    So, the question is, is there any way, that the save operation is completed before the program closes?

    Remark: DoSave is multi-target for wpf, Android, winrt, so it cannot be modified, and a solution only for wpf or winrt is not sufficient.

    Thank you for any help.

    Rowe10

    Monday, December 7, 2015 2:40 AM

Answers

  • i don't think the compiler would happy to see the MainWindow_Closed method has no 'async' modifier marking

    DON'T TRY SO HARD,THE BEST THINGS COME WHEN YOU LEAST EXPECT THEM TO.

    Monday, December 7, 2015 3:11 AM
  • As Matthew suggests you cannot use the await keyword in a method that is not marked async. You could however call your DoSave method synchronously without awaiting it:

    private void MainWindow_Closed(objectsender, EventArgse)
    {
         DoSave();
    }

    Then it will always be executed when the window closes:

     public MainWindow()
            {
                InitializeComponent();
                this.Closed += Window5_Closed;
    }
    
            void MainWindow_Closed(object sender, EventArgs e)
            {
                System.Threading.Thread.Sleep(10000);
                MessageBox.Show("closed");
            }

    If you add the above sample code to your WPF window, run the application and close the window, the MessageBox will appear eventually.


    Hope that helps.

    Please remember to close your threads by marking helpful posts as answer and then start a new thread if you have a new question. Please don't ask several questions in the same thread.

    Monday, December 7, 2015 2:22 PM

All replies

  • i don't think the compiler would happy to see the MainWindow_Closed method has no 'async' modifier marking

    DON'T TRY SO HARD,THE BEST THINGS COME WHEN YOU LEAST EXPECT THEM TO.

    Monday, December 7, 2015 3:11 AM
  • As Matthew suggests you cannot use the await keyword in a method that is not marked async. You could however call your DoSave method synchronously without awaiting it:

    private void MainWindow_Closed(objectsender, EventArgse)
    {
         DoSave();
    }

    Then it will always be executed when the window closes:

     public MainWindow()
            {
                InitializeComponent();
                this.Closed += Window5_Closed;
    }
    
            void MainWindow_Closed(object sender, EventArgs e)
            {
                System.Threading.Thread.Sleep(10000);
                MessageBox.Show("closed");
            }

    If you add the above sample code to your WPF window, run the application and close the window, the MessageBox will appear eventually.


    Hope that helps.

    Please remember to close your threads by marking helpful posts as answer and then start a new thread if you have a new question. Please don't ask several questions in the same thread.

    Monday, December 7, 2015 2:22 PM