You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

C#-UWP中FolderPicker调用PickSingleFolderAsync弹窗冻结问题求助

Fix for FolderPicker Freeze in UWP (Win10 1809 + VS2019)

Hey there, let's break down why your FolderPicker is freezing when trying to select subfolders in the Downloads directory. The core issue lies in how you're handling async/await in your TestAsync method, which creates UI thread context conflicts that break the picker's functionality.

The Root Cause

In your TestAsync method, you start the MyPickFolderAsync task but don't immediately await it before moving to call MyTestAsync. Even if your debugger never reaches the MyTestAsync line, just launching the picker task without awaiting it creates a race condition with the UI thread. UWP's FolderPicker relies entirely on the UI thread's message loop to work properly—when you let another task run in parallel (even if it's not executing yet), it can block the picker from processing user input.

The Fix

Adjust your TestAsync method to ensure you wait for the picker operation to complete (or be canceled) before proceeding with any other async work. Here's the corrected code:

private async void BtnTest_Click(object sender, RoutedEventArgs e) { await TestAsync(); }
private async Task TestAsync() { 
    // Await the folder picker task first to ensure it finishes before moving on
    StorageFolder pickedFolder = await MyPickFolderAsync(); 
    // Only run MyTestAsync if a folder was successfully picked
    if (pickedFolder != null)
    {
        await MyTestAsync(...); 
    }
}
private async Task<StorageFolder> MyPickFolderAsync() { 
    Windows.Storage.Pickers.FolderPicker folderPicker = new Windows.Storage.Pickers.FolderPicker(); 
    folderPicker.SuggestedStartLocation = Windows.Storage.Pickers.PickerLocationId.Downloads; 
    folderPicker.FileTypeFilter.Add("*"); 
    // This await properly captures the UI thread context to keep the picker responsive
    Windows.Storage.StorageFolder folder = await folderPicker.PickSingleFolderAsync(); 
    if (folder != null) { 
        Windows.Storage.AccessCache.StorageApplicationPermissions.FutureAccessList.AddOrReplace("PickedFolderToken", folder); 
        //this.textBlock.Text = "Picked folder: " + folder.Name; 
    } else { 
        //this.textBlock.Text = "Operation cancelled."; 
    } 
    return folder; 
}

Additional Checks to Rule Out Other Issues

  • UI Thread Context: Never call MyPickFolderAsync from a background thread. Since your button click handler is an async void method, it runs on the UI thread by default—but avoid using ConfigureAwait(false) before calling the picker, as that would switch to a non-UI thread and break functionality.
  • Downloads Folder Size: If your Downloads folder has an extremely large number of files/subfolders, it might cause a temporary delay, but this shouldn't result in a full freeze. Test with a smaller folder to rule this out if the issue persists.
  • UWP Permissions: While PickSingleFolderAsync doesn't require special capabilities for the Downloads folder, double-check your app manifest for conflicting permission settings that could restrict folder access.

Why This Works

By awaiting MyPickFolderAsync first, you ensure the FolderPicker runs exclusively on the UI thread without competing tasks. UWP's picker is a modal dialog that needs full access to the UI thread's message loop to process clicks and navigation—splitting or blocking this context is the most common cause of freezes like you're experiencing.

内容的提问来源于stack exchange,提问作者nam

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.13 08:14:36