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