Jarryd
Jarryd

Reputation: 594

UWP FileOpenPicker locks\freezes app in debug

If the debugger is attached, calling this function causes the app to hang. If I run without a debugger, there is no hang, and file pickers work perfectly.

    private async void OnClick(object sender, RoutedEventArgs e)
    {
        FileOpenPicker openPicker = new FileOpenPicker();
    }

I'm certain this is something super simple, but I just don't know.

Edit:

Here's how I'm using it. Keep in mind, that the simpler function creates the hang issue without all my extra code after it. I've stuffed up the image saving, but that's a separate issue I want to debug when I solve what this post is about.

.

   public async Task ImportHeader()
        {
             FileOpenPicker openPicker = new FileOpenPicker();
            openPicker.ViewMode = PickerViewMode.Thumbnail;
            openPicker.SuggestedStartLocation = PickerLocationId.PicturesLibrary;
            openPicker.FileTypeFilter.Add(".jpg");
            openPicker.FileTypeFilter.Add(".png");

            // For multiple image selection
            var files = await openPicker.PickMultipleFilesAsync();
            foreach (StorageFile singleImage in files)
            {
                IRandomAccessStream stream = await singleImage.OpenAsync(Windows.Storage.FileAccessMode.Read);

                var image = new BitmapImage();
                image.SetSource(stream);
                HeaderImage.Source = image;

                //We also save this to disk for later
                Windows.Storage.StorageFolder storageFolder = Windows.Storage.ApplicationData.Current.LocalFolder;
                Windows.Storage.StorageFile file = await storageFolder.CreateFileAsync("header.jpg", Windows.Storage.CreationCollisionOption.ReplaceExisting);

                stream.Seek(0);

                using (StreamWriter bw = new StreamWriter(file.OpenStreamForWriteAsync().Result))
                {
                    var encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.JpegEncoderId, stream);

                    var renderTargetBitmap = new RenderTargetBitmap();
                    await renderTargetBitmap.RenderAsync(HeaderImage, (int)HeaderImage.Width, (int)HeaderImage.Height);
                    var pixels = await renderTargetBitmap.GetPixelsAsync();
                    byte[] bytes = pixels.ToArray();

                    bw.Write(stream);
                }
            }
        }

Upvotes: 0

Views: 361

Answers (1)

Martin Zikmund
Martin Zikmund

Reputation: 39072

This has happened to me as well on some of the recent Windows 10 Insider Preview builds, while the process works flawlessly on stable builds of Windows 10. I think you can assume the cause is there instead of your code.

Upvotes: 2

Related Questions