blob: 4ac39049a1c447a2f4726e8b510b2f02b79fce7c (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
|
---
title: Execute not blocking async shell command in C#
url: non-blocking-shell-exec-csharp.html
date: 2023-05-24
type: notes
draft: false
tags: [csharp, async, shell]
---
Execute a shell command in async in C# while not blocking the UI thread.
```c#
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()`.
```c#
private async void button_Click(object sender, EventArgs e)
{
await executeCopyCommand();
}
```
|