Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8464045
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T14:37:23+00:00 2026-06-10T14:37:23+00:00

For a Windows 8 Application in C/XAML I have to store some data to

  • 0

For a Windows 8 Application in C/XAML I have to store some data to the LocalState folder. I have some trouble using async/await operator I think.

When I am saving datas while the application is running, everything works fine, but when I try to save the data during the OnSuspending method, it seems that my application doesn’t wait for my saving method to be finished to suspend the application.

The stranger thing is that when I’m debugging and doing all the operation step by step slowly everything works fine, but when I don’t put any breakpoints, the application is closed before the data are saved.

Here is my code :

    private async void OnSuspending(object sender, SuspendingEventArgs e)
    {
        var deferral = e.SuspendingOperation.GetDeferral();
        await  api.BeforeClosing(true);
        deferral.Complete();
    }

    public async Task BeforeClosing(Boolean toTombstoning)
    {
            SaveItem<LoadingProgress>(fileNameLoadingProgress, InitLoading);
    }


    public static async void SaveItem<T>(String fileName, T data, Boolean crossThreadSecure = false) where T : IBinarySerializable
    {

            await CreateNewStorageFile(fileName);

            StorageFolder localFolder = Windows.Storage.ApplicationData.Current.LocalFolder;
            StorageFolder dataFolder = await localFolder.GetFolderAsync("Data");

            StorageFile file = await dataFolder.GetFileAsync(fileName);

            using (var stream = await file.OpenStreamForWriteAsync())
            {
                using (var writer = new BinaryWriter(stream))
                {
                    writer.Write<T>(data);
                }
            }
    }

    public async static Task CreateNewStorageFile(string filename)
    {
        StorageFolder localFolder = Windows.Storage.ApplicationData.Current.LocalFolder;
        StorageFolder dataFolder = await localFolder.CreateFolderAsync("Data", CreationCollisionOption.OpenIfExists);
        storage = await dataFolder.CreateFileAsync(filename, CreationCollisionOption.OpenIfExists);
    }

Am I doing something wrong with the await operator? Or is there a maximum amount of time when we can do some operations before suspending the application?

Thank you

EDIT : I made some new tests : If I remove deferral.Complete(); from the OnSuspending method, it saves the data well (but never close the app obviously…). I really think the problem is that I run out of time when the application is suspending. It really looks like the problem on this thread : StorageFolder.CreateFileAsync crashes when called from App.OnSuspending. But I have tried all the solutions of this question and I still have the problem…

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T14:37:25+00:00Added an answer on June 10, 2026 at 2:37 pm

    Your problem is that you’re calling Complete before all your async operations have completed.

    The simplest solution is to make SaveItem an async Task method instead of async void, and then await it in BeforeClosing.

    As a general rule, async methods should always return Task/Task<T> unless they have to return void (e.g., event handlers).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have created blank C#/XAML Windows 8 application. Add simple XAML code: <Page x:Class=Blank.MainPage
I'm developing a Windows Phone application . I have defined this on App.xaml: <nav:UriMapping
I have a Windows Phone application which gets data from a SQL server with
I'm creating a windows 8 application (but I suspect that any one using Xaml
I have a windows application written in VB.net. After finishing it I'll setup the
I have one .net windows application I'm trying to read .xml file from c#
I have created Windows Application. In this, I have multiple tables in dataset, now
I am using the Visifire charts to display data on a Windows Phone 7
I have a multi page windows phone application. I have a main page and
I have a simple WPF windows application trying to read a serial port with

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.