C#C
C#2y ago
Mayka

✅ Lag after closing folder dialog in console application

I need to raise a folder dialog within a console application. It works if I add the [STAThread attribute to the main method. However, there’s consistently a 3-5 second delay after the folder dialog is closed.

This Stack Overflow question bring up a similar issue for OpenFileDialog but without a concrete resolution.
https://stackoverflow.com/questions/41894044/closing-a-openfiledialog-causes-application-to-hang-for-3-4-seconds

Is there any way to remove or minimize the delay after the folder dialog is closed?

Project Settings

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net8.0-windows</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
    <UseWPF>true</UseWPF>
  </PropertyGroup>
</Project>

Code

using Microsoft.Win32;
 
internal class Program
{
    [STAThread]
    private static void Main()
    {
        OpenFolderDialog folderDialog = new();
 
        if (folderDialog.ShowDialog() is true)
        {
            string mainFolderPath = folderDialog.FolderName;
            Console.WriteLine($"Selected folder: {mainFolderPath}");
        }
 
        Console.WriteLine("\nPress any key to continue...");
        Console.ReadKey();
    }
}
Stack Overflow
In my WPF application which uses the Caliburn.Micro and MahApps.Metro frameworks, i open instances of OpenFileDialog from the System.Windows.Forms namespace. After opening and closing this dialog, ...
Was this page helpful?