Почему WinForms так медленно выполняет код?

Я протестировал два приложения, которые находят файлы по названию, одно консольное, а второе на WinForms. Консольное приложение находит файлы за 14 секунд, а на WinForms за 58 секунд. Почему так долго выполняется? Вот WinForms:

async Task FindFiles()
{
    await Task.Run(() => 
    {
        if (!string.IsNullOrEmpty(path_textbox.Text) && !string.IsNullOrEmpty(searchword_textbox.Text))
        {
            string path = path_textbox.Text;
            string word = searchword_textbox.Text;

            if (File.Exists(desktop))
            {
                File.Delete(desktop);
            }

            log_textbox.Text = ">>> SEARCH STARTED <<<\r\n\r\n";

            try
            {
                IEnumerable<string> found = Directory.EnumerateFiles(path, $"*{word}*", new EnumerationOptions { IgnoreInaccessible = true, RecurseSubdirectories = true });
                if (found.Any())
                {
                    if (save_checkbox.Checked)
                    {
                        File.AppendAllText(temp, $"Path: \"{path}\";\nFileName: \"{word}\";\n\n");
                    }

                    timer.Start();
                    foreach (string file in found)
                    {
                        if (save_checkbox.Checked)
                        {
                            File.AppendAllText(temp, $"{i}) Found: \"{file}\" - {DateTime.Now.ToString("HH:mm:ss")};\n");
                        }

                        log_textbox.AppendText($"{i}) Found: \"{file}\"\r\n");

                        log_textbox.SelectionStart = log_textbox.TextLength;
                        log_textbox.ScrollToCaret();

                        i++;
                    }
                    timer.Stop();

                    i--;
                            
                    log_textbox.AppendText($"\r\n> Found {i} file(s) in {timer.Elapsed.TotalSeconds:F2} seconds <");

                    if (save_checkbox.Checked)
                    {
                        log_textbox.AppendText($"> The file with the results is saved on your desktop ({new FileInfo(temp).Length} bytes) <");
                        File.AppendAllText(temp, $"\nFound {i} file(s) in {timer.Elapsed.TotalSeconds:F2} seconds;");
                        File.Copy(temp, desktop);
                    }
                }
                else
                {
                    log_textbox.Text = "> Nothing was found <";
                }
            }
            catch (Exception)
            {
                log_textbox.Text = "> An error occurred <";
            }
        }
    });
}

А вот консольное:

try
{
    IEnumerable<string> found = Directory.EnumerateFiles($"{path}", $"*{fileName}*", new EnumerationOptions { IgnoreInaccessible = true, RecurseSubdirectories = true });
    if (found.Any())
    {
        timer.Start();
        foreach (string file in found)
        {
            File.AppendAllText(temp, $"{i}) Found: \"{file}\" - {DateTime.Now.ToString("HH:mm:ss")};\n");
            Console.WriteLine($"{i}) Found: \"{file}\"");
            i++;
        }
        timer.Stop();

        i--;
        File.AppendAllText(temp, $"\nFound {i} file(s) in {timer.Elapsed.TotalSeconds:F2} seconds;");
        Console.WriteLine($"\n> Found {i} file(s) in {timer.Elapsed.TotalSeconds:F2} seconds <");

        SaveResults();
    }
    else
    {
        Console.WriteLine("\n> Nothing was found <\n\n-----\n");
    }
}
catch (Exception)
{
    Console.WriteLine("\n> An error occurred <\n\n-----\n");
}

Связано ли это как-то с Task или с тем, что это графическое приложение? Помогите, пожалуйста!


Ответы (0 шт):