diff options
| author | Mitja Felicijan <m@mitjafelicijan.com> | 2023-07-16 22:46:06 +0200 |
|---|---|---|
| committer | Mitja Felicijan <m@mitjafelicijan.com> | 2023-07-16 22:46:11 +0200 |
| commit | 0cb6a5c81271a61e930505f3315b1d67bdf22724 (patch) | |
| tree | 27df39e2004dda656f5e8ae09e9d4e6395d4f903 /content/notes/non-blocking-shell-exec-csharp.md | |
| parent | 89b2019bfa3bd8f1f0ceb7724c492fdf337dd519 (diff) | |
| download | mitjafelicijan.com-0cb6a5c81271a61e930505f3315b1d67bdf22724.tar.gz | |
Renamed all the notes files to include date
Diffstat (limited to 'content/notes/non-blocking-shell-exec-csharp.md')
| -rw-r--r-- | content/notes/non-blocking-shell-exec-csharp.md | 44 |
1 files changed, 0 insertions, 44 deletions
diff --git a/content/notes/non-blocking-shell-exec-csharp.md b/content/notes/non-blocking-shell-exec-csharp.md deleted file mode 100644 index ffad85c..0000000 --- a/content/notes/non-blocking-shell-exec-csharp.md +++ /dev/null | |||
| @@ -1,44 +0,0 @@ | |||
| 1 | --- | ||
| 2 | title: Execute not blocking async shell command in C# | ||
| 3 | url: non-blocking-shell-exec-csharp.html | ||
| 4 | date: 2023-05-22T12:00:00+02:00 | ||
| 5 | type: note | ||
| 6 | draft: false | ||
| 7 | tags: [csharp] | ||
| 8 | --- | ||
| 9 | |||
| 10 | Execute a shell command in async in C# while not blocking the UI thread. | ||
| 11 | |||
| 12 | ```c# | ||
| 13 | private 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 | |||
| 35 | Make sure that `async` is present in the function definition and `await` is used | ||
| 36 | in the method that calls `executeCopyCommand()`. | ||
| 37 | |||
| 38 | ```c# | ||
| 39 | private async void button_Click(object sender, EventArgs e) | ||
| 40 | { | ||
| 41 | await executeCopyCommand(); | ||
| 42 | } | ||
| 43 | ``` | ||
| 44 | |||
