aboutsummaryrefslogtreecommitdiff
path: root/content/notes/2023-05-22-non-blocking-shell-exec-csharp.md
diff options
context:
space:
mode:
Diffstat (limited to 'content/notes/2023-05-22-non-blocking-shell-exec-csharp.md')
-rw-r--r--content/notes/2023-05-22-non-blocking-shell-exec-csharp.md43
1 files changed, 43 insertions, 0 deletions
diff --git a/content/notes/2023-05-22-non-blocking-shell-exec-csharp.md b/content/notes/2023-05-22-non-blocking-shell-exec-csharp.md
new file mode 100644
index 0000000..afe7491
--- /dev/null
+++ b/content/notes/2023-05-22-non-blocking-shell-exec-csharp.md
@@ -0,0 +1,43 @@
1---
2title: Execute not blocking async shell command in C#
3url: /non-blocking-shell-exec-csharp.html
4date: 2023-05-22T12:00:00+02:00
5type: note
6draft: false
7---
8
9Execute a shell command in async in C# while not blocking the UI thread.
10
11```c#
12private async Task executeCopyCommand()
13{
14 await Task.Run(() =>
15 {
16 var processStartInfo = new ProcessStartInfo("cmd", "/c dir")
17 {
18 RedirectStandardOutput = true,
19 UseShellExecute = false,
20 CreateNoWindow = true
21 };
22
23 var process = new Process
24 {
25 StartInfo = processStartInfo
26 };
27
28 process.Start();
29 process.WaitForExit();
30 });
31}
32```
33
34Make sure that `async` is present in the function definition and `await` is used
35in the method that calls `executeCopyCommand()`.
36
37```c#
38private async void button_Click(object sender, EventArgs e)
39{
40 await executeCopyCommand();
41}
42```
43