Skip to content

Commit 423cf38

Browse files
committed
fix: single instance modification allows processes opened during the dialog
1 parent db34965 commit 423cf38

1 file changed

Lines changed: 40 additions & 23 deletions

File tree

FlashpointSecurePlayer/SingleInstance.cs

Lines changed: 40 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,46 @@ public override void Activate(string templateName) {
106106
bool clear = false;
107107

108108
do {
109+
if (processesByNameStrict != null) {
110+
// don't allow preceding further until
111+
// all processes with the same name have been killed
112+
DialogResult ? dialogResult = ShowClosableMessageBox(
113+
Task.Run(delegate () {
114+
// copy this, so it doesn't get set to null upon hitting OK
115+
Stack<Process> _processesByNameStrict = new Stack<Process>(processesByNameStrict);
116+
117+
while (_processesByNameStrict.Any()) {
118+
using (Process processByNameStrict = _processesByNameStrict.Pop()) {
119+
try {
120+
if (processByNameStrict != null) {
121+
processByNameStrict.WaitForExit();
122+
}
123+
} catch {
124+
// fail silently
125+
// (ensure we dispose every process)
126+
}
127+
}
128+
}
129+
}),
130+
131+
String.Format(
132+
Properties.Resources.ProcessCompatibilityConflict,
133+
activeProcessName
134+
),
135+
136+
Properties.Resources.FlashpointSecurePlayer,
137+
MessageBoxButtons.OKCancel,
138+
MessageBoxIcon.Warning
139+
);
140+
141+
if (dialogResult == DialogResult.Cancel) {
142+
Application.Exit();
143+
throw new InvalidModificationException("The operation was aborted by the user.");
144+
}
145+
146+
processesByNameStrict = null;
147+
}
148+
109149
// the strict list is the one which will be checked against for real
110150
processesByName = Process.GetProcessesByName(activeProcessName);
111151

@@ -144,29 +184,6 @@ public override void Activate(string templateName) {
144184
}
145185

146186
processesByName = null;
147-
148-
// don't allow preceding further until
149-
// all processes with the same name have been killed
150-
if (processesByNameStrict.Any()) {
151-
DialogResult? dialogResult = ShowClosableMessageBox(Task.Run(delegate () {
152-
while (processesByNameStrict.Any()) {
153-
using (Process processByNameStrict = processesByNameStrict.Peek()) {
154-
try {
155-
if (processByNameStrict != null) {
156-
processByNameStrict.WaitForExit();
157-
}
158-
} finally {
159-
processesByNameStrict.Pop();
160-
}
161-
}
162-
}
163-
}), String.Format(Properties.Resources.ProcessCompatibilityConflict, activeProcessName), Properties.Resources.FlashpointSecurePlayer, MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
164-
165-
if (dialogResult == DialogResult.Cancel) {
166-
Application.Exit();
167-
throw new InvalidModificationException("The operation was aborted by the user.");
168-
}
169-
}
170187
// continue this process until the problem is resolved
171188
} while (processesByNameStrict.Any());
172189
}

0 commit comments

Comments
 (0)