diff options
| author | Mitja Felicijan <mitja.felicijan@gmail.com> | 2024-03-10 14:59:14 +0100 |
|---|---|---|
| committer | Mitja Felicijan <mitja.felicijan@gmail.com> | 2024-03-10 14:59:14 +0100 |
| commit | 1100562e29f6476448b656dbddd4cf22505523f6 (patch) | |
| tree | 442eec492199104bd49dfd74474ce89ade8fcac9 /_posts/notes/2023-05-22-non-blocking-shell-exec-csharp.md | |
| parent | a40d80be378e46a6c490e1b99b0d8f4acd968503 (diff) | |
| download | mitjafelicijan.com-1100562e29f6476448b656dbddd4cf22505523f6.tar.gz | |
Move back to JBMAFP
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.md | 45 |
1 files changed, 0 insertions, 45 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 deleted file mode 100644 index f8b9c53..0000000 --- a/_posts/notes/2023-05-22-non-blocking-shell-exec-csharp.md +++ /dev/null | |||
| @@ -1,45 +0,0 @@ | |||
| 1 | --- | ||
| 2 | title: Execute not blocking async shell command in C# | ||
| 3 | permalink: /non-blocking-shell-exec-csharp.html | ||
| 4 | date: 2023-05-22T12:00:00+02:00 | ||
| 5 | layout: post | ||
| 6 | type: note | ||
| 7 | draft: false | ||
| 8 | tags: [csharp] | ||
| 9 | --- | ||
| 10 | |||
| 11 | Execute a shell command in async in C# while not blocking the UI thread. | ||
| 12 | |||
| 13 | ```c# | ||
| 14 | private 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 | |||
| 36 | Make sure that `async` is present in the function definition and `await` is used | ||
| 37 | in the method that calls `executeCopyCommand()`. | ||
| 38 | |||
| 39 | ```c# | ||
| 40 | private async void button_Click(object sender, EventArgs e) | ||
| 41 | { | ||
| 42 | await executeCopyCommand(); | ||
| 43 | } | ||
| 44 | ``` | ||
| 45 | |||
