Repository navigation
Expand file tree
/
Copy pathProgram.cs
More file actions
147 lines (119 loc) · 3.54 KB
/
Copy pathProgram.cs
File metadata and controls
147 lines (119 loc) · 3.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
using LTRData.Extensions.CommandLine;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
namespace waitps;
public static class Program
{
private static readonly string[] exeFileExtensions = [".exe", ".com", ".dll"];
public static int Main(params string[] args)
{
var cmds = CommandLineParser.ParseCommandLine(args, StringComparer.Ordinal);
var waitAny = false;
string[] processes = [];
foreach (var cmd in cmds)
{
switch (cmd.Key)
{
case "a":
waitAny = true;
break;
case "":
processes = cmd.Value;
break;
default:
Console.WriteLine($@"Process wait utility - Copyright (c) 2024-2026 Olof Lagerkvist, LTR Data
https://ltr-data.se/opencode.html
Syntax:
waitps [-a] pid1 [pid2 ...]
-a Wait for any of specified processes to exit. Otherwise, waits for all
of specified processes to exit.
Processes can be specified as numeric process id or process name.");
return -1;
}
}
var pslist = new List<Process>();
try
{
foreach (var process in processes)
{
if (int.TryParse(process, out var pid))
{
pslist.Add(Process.GetProcessById(pid));
}
else
{
var psName = process;
if (exeFileExtensions.Contains(Path.GetExtension(psName), StringComparer.OrdinalIgnoreCase))
{
psName = psName.Remove(psName.Length - 4);
}
pslist.AddRange(Process.GetProcessesByName(psName));
}
}
if (pslist.Count == 0)
{
Console.WriteLine("No process found");
return 0;
}
foreach (var ps in pslist)
{
Console.WriteLine($"{ps.Id,7} {ps.ProcessName}");
}
if (pslist.Count == 1)
{
Console.WriteLine("Waiting for exit.");
}
else if (waitAny)
{
Console.WriteLine("Waiting for any to exit.");
}
else
{
Console.WriteLine("Waiting for all to exit.");
}
if (waitAny)
{
var tasks = pslist
.Select(ps => ps.WaitForExitAsync())
.ToArray();
Task.WaitAny(tasks);
}
else
{
foreach (var ps in pslist)
{
ps.WaitForExit();
}
}
var psExitCount = 0;
foreach (var ps in pslist)
{
if (ps.HasExited)
{
psExitCount++;
Console.WriteLine($"{ps.Id,7} exit");
}
}
return psExitCount;
}
catch (Exception ex)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.Error.WriteLine(ex.GetBaseException().Message);
Console.ResetColor();
return ex.HResult;
}
finally
{
pslist.ForEach(ps => ps.Dispose());
}
}
#if !NET5_0_OR_GREATER
private static Task WaitForExitAsync(this Process ps)
=> Task.Run(ps.WaitForExit);
#endif
}