Budzique
Budzique
CC#
Created by Budzique on 3/20/2025 in #help
EnumerateFiles Blocks UI thread even in Task.Run()
entire app becomes unresponsive
11 replies
CC#
Created by Budzique on 3/20/2025 in #help
EnumerateFiles Blocks UI thread even in Task.Run()
This is a functional example from my main project and all it does is basically show a green message for validation. It seems like such a headache but userfriendliness is my goal. ugh. Any alternative methods to achieve the same goal without blocking the UI thread?
11 replies
CC#
Created by Budzique on 3/20/2025 in #help
EnumerateFiles Blocks UI thread even in Task.Run()
Also, adding async to button_Click and await to LiveryValidate didn't change, still locks the UI while scanning.
11 replies
CC#
Created by Budzique on 3/20/2025 in #help
EnumerateFiles Blocks UI thread even in Task.Run()
Its odd, I think it looks right too, but it can't be like a .net bug can it?
11 replies
CC#
Created by Budzique on 3/20/2025 in #help
EnumerateFiles Blocks UI thread even in Task.Run()
11 replies
CC#
Created by Budzique on 3/20/2025 in #help
EnumerateFiles Blocks UI thread even in Task.Run()
<Window x:Class="threadTest.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:threadTest"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid>
<Button x:Name="button" Content="Button" HorizontalAlignment="Left" Margin="585,69,0,0" VerticalAlignment="Top" Click="button_Click"/>
<TextBox x:Name="textBox" HorizontalAlignment="Left" Margin="235,74,0,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="310" Height="20"/>

</Grid>
</Window>
<Window x:Class="threadTest.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:threadTest"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid>
<Button x:Name="button" Content="Button" HorizontalAlignment="Left" Margin="585,69,0,0" VerticalAlignment="Top" Click="button_Click"/>
<TextBox x:Name="textBox" HorizontalAlignment="Left" Margin="235,74,0,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="310" Height="20"/>

</Grid>
</Window>
11 replies
CC#
Created by Budzique on 3/20/2025 in #help
EnumerateFiles Blocks UI thread even in Task.Run()
using Microsoft.Win32;
using System.IO;
using System.Windows;

namespace threadTest
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}

private async Task LiveryValidate(string folderName, string extensionToFind)
{
await Task.Run(() =>
{
try
{
IEnumerable<string>? ddsFiles = Directory.EnumerateFiles(folderName, "*."+extensionToFind, SearchOption.AllDirectories);
string fullpath = ddsFiles.First().ToString();
string dirname = Path.GetDirectoryName(fullpath)!;
if (dirname != null)
{
Console.WriteLine("Found");
}
}
catch (Exception ex)
{
Console.WriteLine("Not Found");
Console.WriteLine(ex.Message);
}
});
}

private void button_Click(object sender, RoutedEventArgs e)
{
OpenFolderDialog folderDialog = new OpenFolderDialog();
if (folderDialog.ShowDialog() == true)
{
string? folderName = folderDialog.FolderName;
textBox.Text = folderName;
_ = LiveryValidate(folderName, "DDS");

}
}
}
}
using Microsoft.Win32;
using System.IO;
using System.Windows;

namespace threadTest
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}

private async Task LiveryValidate(string folderName, string extensionToFind)
{
await Task.Run(() =>
{
try
{
IEnumerable<string>? ddsFiles = Directory.EnumerateFiles(folderName, "*."+extensionToFind, SearchOption.AllDirectories);
string fullpath = ddsFiles.First().ToString();
string dirname = Path.GetDirectoryName(fullpath)!;
if (dirname != null)
{
Console.WriteLine("Found");
}
}
catch (Exception ex)
{
Console.WriteLine("Not Found");
Console.WriteLine(ex.Message);
}
});
}

private void button_Click(object sender, RoutedEventArgs e)
{
OpenFolderDialog folderDialog = new OpenFolderDialog();
if (folderDialog.ShowDialog() == true)
{
string? folderName = folderDialog.FolderName;
textBox.Text = folderName;
_ = LiveryValidate(folderName, "DDS");

}
}
}
}
11 replies