Execute not blocking async shell command in C#

note, May 22, 2023, on Mitja Felicijan's blog

Execute a shell command in async in C# while not blocking the UI thread.

private async Task executeCopyCommand()
{
  await Task.Run(() =>
  {
    var processStartInfo = new ProcessStartInfo("cmd", "/c dir")
    {
      RedirectStandardOutput = true,
      UseShellExecute = false,
      CreateNoWindow = true
    };

    var process = new Process
    {
      StartInfo = processStartInfo
    };

    process.Start();
    process.WaitForExit();
  });
}

Make sure that async is present in the function definition and await is used in the method that calls executeCopyCommand().

private async void button_Click(object sender, EventArgs e)
{
  await executeCopyCommand();
}

Other notes

DateTitle
Cronjobs on Github with Github Actions
Dungeon Crawl Stone Soup - New player guidedcss
Push to multiple origins at once in Git
Display xterm color palette
Make DCSS playable on 4k displaysdcss
Mount Plan9 over networkplan9
Run 9front in Qemuplan9
Drawing Pixels in Plan9plan9
Write and read structs to/from files in Cc
Sane default for Dungeon Crawl Stone Soup Online editiondcss
Using ffmpeg to combine videos side by side
Making cgit look nicer
Make B/W SVG charts with matplotlib
Parse RSS feeds with Lua
Uninstall Ollama from a Linux box