aboutsummaryrefslogtreecommitdiff
path: root/content/notes/non-blocking-shell-exec-csharp.md
diff options
context:
space:
mode:
authorMitja Felicijan <m@mitjafelicijan.com>2023-07-12 18:35:08 +0200
committerMitja Felicijan <m@mitjafelicijan.com>2023-07-12 18:35:08 +0200
commit23a56bd50b04211da3cab45f72c3390711b2416b (patch)
treeab9a4a0136b4cce06dba7d853e296f682f807dbb /content/notes/non-blocking-shell-exec-csharp.md
parentcecb4b48a39a3558979b9c4b50e45bf605a3684e (diff)
downloadmitjafelicijan.com-23a56bd50b04211da3cab45f72c3390711b2416b.tar.gz
Moved notes and posts into subfolders
Diffstat (limited to 'content/notes/non-blocking-shell-exec-csharp.md')
-rw-r--r--content/notes/non-blocking-shell-exec-csharp.md44
1 files changed, 44 insertions, 0 deletions
diff --git a/content/notes/non-blocking-shell-exec-csharp.md b/content/notes/non-blocking-shell-exec-csharp.md
new file mode 100644
index 0000000..ffad85c
--- /dev/null
+++ b/content/notes/non-blocking-shell-exec-csharp.md
@@ -0,0 +1,44 @@
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
7tags: [csharp]
8---
9
10Execute a shell command in async in C# while not blocking the UI thread.
11
12```c#
13private async Task executeCopyCommand()
14{
15 await Task.Run(() =>
16 {
17 var processStartInfo = new ProcessStartInfo("cmd", "/c dir")
18 {
19 RedirectStandardOutput = true,
20 UseShellExecute = false,
21 CreateNoWindow = true
22 };
23
24 var process = new Process
25 {
26 StartInfo = processStartInfo
27 };
28
29 process.Start();
30 process.WaitForExit();
31 });
32}
33```
34
35Make sure that `async` is present in the function definition and `await` is used
36in the method that calls `executeCopyCommand()`.
37
38```c#
39private async void button_Click(object sender, EventArgs e)
40{
41 await executeCopyCommand();
42}
43```
44