locked
CreateFileAsync hangs during Share contract operation

    Question

  • I use the code below to write an InMemoryRandomAccessStream to disk.  Unfortunately when the CreateFileAsync() call is made the app hangs and I need to stop the debugging session completely.  I thought it might be a classic UI thread/continuation context deadlock, but CreateFileAsync() does not support the ConfigureAwait() method so I don't know what to do to test that theory.  Here's the code:

            public static async Task<StorageFile> ToStorageFile_pictures(this InMemoryRandomAccessStream ras, string fileName)
            {
                if (ras == null)
                    throw new ArgumentNullException("The random access memory stream is unassigned.");
    
                if (String.IsNullOrWhiteSpace(fileName))
                    throw new ArgumentNullException("The file name is empty.");
    
                // Create file in Pictures library (for now) and write bitmap to it after converting it to JPEG format.
                StorageFile outputFile = await KnownFolders.PicturesLibrary.CreateFileAsync(fileName, CreationCollisionOption.ReplaceExisting);
    
                using (IRandomAccessStream writeStream = await outputFile.OpenAsync(FileAccessMode.ReadWrite))
                {
                    await RandomAccessStream.CopyAndCloseAsync(ras.GetInputStreamAt(0), writeStream.GetOutputStreamAt(0));
                }
    
                return outputFile;
            }

    UPDATE: I found the AsTask() method so I can use ConfigureAwait()false):

    StorageFile outputFile = await KnownFolders.PicturesLibrary.CreateFileAsync(fileName, CreationCollisionOption.ReplaceExisting).AsTask().ConfigureAwait(false);

    I can get trace past the CreateFileAsync() call now but it still deadlocks.  This makes sense to me because I really do need the result output of CreateFileAsync() before continuing. But I still don't know how to solve the deadlock.  I'll try pushing it off to another thread but that seems like an awkward solution.

    UPDATE 2: The implementation below gets me the furthest, but now it hangs on the when at the end of the lambda method.  It really looks like a deadlock due to a UI thread continuation context issue:

    await Windows.System.Threading.ThreadPool.RunAsync(async (wiSender) =>
                {
                    outputFile = await KnownFolders.PicturesLibrary.CreateFileAsync(fileName, CreationCollisionOption.ReplaceExisting);
    
                    using (IRandomAccessStream writeStream = await outputFile.OpenAsync(FileAccessMode.ReadWrite))
                    {
                        await RandomAccessStream.CopyAndCloseAsync(ras.GetInputStreamAt(0), writeStream.GetOutputStreamAt(0));
                    }
                }, Windows.System.Threading.WorkItemPriority.Normal);


    -- roschler






    • Edited by roschler Wednesday, February 26, 2014 2:13 PM
    Wednesday, February 26, 2014 6:02 AM

All replies

  • I'm posting this answer here in case it helps others.  The problem appears to be trying to call CreateFileAsync() while inside the method that creates content for a Share contract data request with your app as the Share source.  I moved the code that calls my ToStorageFile_pictures() method into an async method that I don't await and the deadlock went away.  I guess there are certain things you can't do from a Share content operation.

    -- roschler

    Wednesday, February 26, 2014 2:16 PM