C
C#10mo 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>
<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();
    }
}
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
Closing a OpenFileDialog causes application to hang for 3-4 seconds
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, ...
12 Replies
leowest
leowest10mo ago
@Mayka what about using FolderBrowserDialog
leowest
leowest10mo ago
No description
Mayka
MaykaOP10mo ago
@leowest I seem to have the same issue with FolderBrowserDialog. I also tried running the compiled exe outside of VS in case the debugger was to blame, but it has the same lag.
No description
leowest
leowest10mo ago
sounds like an isolated issue you're having yeah I tried a few different things but I cannot reproduce your delay
Mayka
MaykaOP10mo ago
@leowest what OS version are you on? Trying to figure out if this is an OS issue.
leowest
leowest10mo ago
No description
Mayka
MaykaOP10mo ago
@leowest well we’re actually on the exact same OS build, so I guess that’s not it :OOF:
leowest
leowest10mo ago
and I have not reinstalled since 2020 so 😛 @Mayka do u have antivirus that could be potentially doing some realtime check and it could maybe catch that build and delay it some times AV does weird stuff
Mayka
MaykaOP10mo ago
@leowest this is a work computer, but it’s definitely an issue with mine specifically. I had my coworker try the same exe and they didn’t have a delay either. UGH!! I hate when I chase my tail after the completely wrong thing. My hunch tells me it has to do with the same issue I experience where if I create a new folder or delete a file in File Explorer, the changes never seem to show up for me until I hit the Refresh button. I thought maybe it was my local icon cache, but deleting that didn’t fix the issue.
leowest
leowest10mo ago
@Mayka can u try resetting the theme of your windows? kind random but
Mayka
MaykaOP10mo ago
@leowest after extensive troubleshooting, OneDrive seems likely to have been the culprit, possibly due to a corrupted SharePoint shortcut. Since removing it, File Explorer is auto-synching again with file system changes, and my C# code is no longer hanging after I’ve closed the folder dialog. Thank you very much for your help with this! Determining it was a setup issue rather than a C# issue helped lead me down a path toward a solution.
leowest
leowest10mo ago
no worries thanks for letting me know what the cause was dont forget to throw a /close here 😉
Want results from more Discord servers?
Add your server