Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The problem is with <code>await</code> keyword. In short, <code>await</code> really doesn't block the executing thread, as it may sound. Quite the opposite, when execution comes to <code>await</code> keyword, it looks like <code>return</code> from the caller's point of view. The continuation (what comes after <code>await</code> keyword) is scheduled to run later, when the task associated with <code>await</code> is completed. For more details about that, I encourage you to read more about <code>await</code> and Task, starting, for example, <a href="http://msdn.microsoft.com/en-us/library/vstudio/hh156528.aspx" rel="noreferrer">here</a>.</p> <p>In your case, when Deactivated or Closing is called, you probably make a call similar to:</p> <pre><code>private void Application_Deactivated(object sender, DeactivatedEventArgs e) { CacheData(); } </code></pre> <p>When execution comes to the first <code>await</code> keyword (that is, to the line <code>StorageFile storageFileIsolated = await localFolder.CreateFileAsync("Data.data", CreationCollisionOption.ReplaceExisting);</code>), your <code>CacheData()</code> method returns control to the caller. Everything else will run later, when that task is completed. But you're not waiting for that in your <code>Application_Deactivated</code> method. Instead, execution continues and control leaves the <code>Application_Deactivated</code>.</p> <p>When control leaves the Application_Deactivated, your app is considered to be ready to shutdown. But in fact it's not ready, because <code>await writer.WriteAsync(data);</code> is probably not even started yet. That's why your app closes without writing anything.</p> <p>Btw, if your app is not tombstoned during deactivation, as if you return to it quickly, the <code>WriteAsync</code> should be completed eventually. But of course it's not what you expect.</p> <p>In order to fix this, you need to run your file write operation synchronously. By the way, this is precisely what old API does, and that's why it works. The easiest way to fix your problem would be to change your <code>CacheData</code> function to return <code>Task</code> instead of <code>void</code> (to <code>public async Task CacheData()</code>), and use <code>Task.Wait()</code> in your <code>Application_Deactivated</code> handler. Like this:</p> <pre><code>public async Task CacheData() { StorageFolder localFolder = ApplicationData.Current.LocalFolder; string data = "Hello!"; ///WinRT not Working !!! // Get a reference to the Local Folder // Create the file in the local folder, or if it already exists, just replace it StorageFile storageFileIsolated = await localFolder.CreateFileAsync("Data.data", CreationCollisionOption.ReplaceExisting); Stream writeStream = await storageFileIsolated.OpenStreamForWriteAsync(); using (StreamWriter writer = new StreamWriter(writeStream)) { await writer.WriteAsync(data); } } private void Application_Deactivated(object sender, DeactivatedEventArgs e) { CacheData().Wait(); } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload