-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInstallUpdate.Processes.cs
More file actions
214 lines (178 loc) · 7.89 KB
/
Copy pathInstallUpdate.Processes.cs
File metadata and controls
214 lines (178 loc) · 7.89 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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.IO;
using System.ServiceProcess;
using System.Text;
using System.Threading;
using wyUpdate.Common;
namespace wyUpdate
{
partial class InstallUpdate
{
public void RunProcessesCheck()
{
bw.DoWork += bw_DoWorkProcessesCheck;
bw.ProgressChanged += bw_ProgressChanged;
bw.RunWorkerCompleted += bw_RunWorkerCompletedProcessesCheck;
bw.RunWorkerAsync();
}
void bw_DoWorkProcessesCheck(object sender, DoWorkEventArgs e)
{
// processes
List<FileInfo> files = null;
List<Process> rProcesses = null;
// rollback list for stopped services
List<string> stoppedServices = new List<string>();
Exception except = null; // store any errors
// create the backup folder
Directory.CreateDirectory(Path.Combine(TempDirectory, "backup"));
try
{
// if we're AutoUpdating the service has been shutdown by the AutomaticUpdaterBackend
// and we should wait for the service to complete shutting down
if (SkipStartService != null)
{
using (ServiceController srvc = new ServiceController(SkipStartService))
{
if (srvc.Status != ServiceControllerStatus.Stopped)
srvc.WaitForStatus(ServiceControllerStatus.Stopped);
}
}
// first try to stop services
foreach (string service in UpdtDetails.ServicesToStop)
{
using (ServiceController srvc = new ServiceController(service))
{
ServiceControllerStatus status = ServiceControllerStatus.Stopped;
try
{
// non-existent services throw an exception on queries
status = srvc.Status;
}
catch { }
if (status == ServiceControllerStatus.Running)
{
try
{
srvc.Stop();
}
catch (Exception)
{
// get the latest status of the service -- it might have crashed close
srvc.Refresh();
if (srvc.Status != ServiceControllerStatus.Stopped)
throw;
}
// report that we're waiting for the service to stop so the user knows what's going on
bw.ReportProgress(0, new object[] { -1, -1, "Waiting for service to stop: " + srvc.DisplayName, ProgressStatus.None, null });
srvc.WaitForStatus(ServiceControllerStatus.Stopped);
stoppedServices.Add(service);
}
}
}
files = new List<FileInfo>(new DirectoryInfo(ProgramDirectory).GetFiles("*.exe", SearchOption.AllDirectories));
RemoveSelfFromProcesses(files);
//check for (and delete) a newer client if it exists
DeleteClientInPath(ProgramDirectory, Path.Combine(TempDirectory, "base"));
rProcesses = ProcessesNeedClosing(files);
if (rProcesses.Count == 0)
{
// no processes need closing, all done
files = null;
rProcesses = null;
}
else if (SkipUIReporting) // and rProcesses.Count > 0
{
// check every second for 20 seconds.
for (int i = 0; i < 20; ++i)
{
// sleep for 1 second
Thread.Sleep(1000);
rProcesses = ProcessesNeedClosing(files);
if (rProcesses.Count == 0)
break;
}
if (rProcesses.Count != 0)
{
StringBuilder sb = new StringBuilder();
sb.AppendLine(rProcesses.Count + " processes are running:\r\n");
foreach (Process proc in rProcesses)
{
sb.AppendLine(proc.MainWindowTitle + " (" + proc.ProcessName + ".exe)");
}
// tell the user about the open processes
throw new Exception(sb.ToString());
}
}
}
catch (Exception ex)
{
except = ex;
}
// save rollback info
RollbackUpdate.WriteRollbackServices(Path.Combine(TempDirectory, "backup\\stoppedServices.bak"), stoppedServices);
if (IsCancelled() || except != null)
{
bw.ReportProgress(1, false);
// rollback stopped services
RollbackUpdate.RollbackStoppedServices(TempDirectory);
bw.ReportProgress(0, new object[] { -1, -1, string.Empty, ProgressStatus.Failure, except });
}
else // completed successfully
{
bw.ReportProgress(0, new object[] { -1, -1, string.Empty, ProgressStatus.Success, new object[] { files, rProcesses } });
}
}
void bw_RunWorkerCompletedProcessesCheck(object sender, RunWorkerCompletedEventArgs e)
{
bw.DoWork -= bw_DoWorkProcessesCheck;
bw.ProgressChanged -= bw_ProgressChanged;
bw.RunWorkerCompleted -= bw_RunWorkerCompletedProcessesCheck;
}
static void RemoveSelfFromProcesses(List<FileInfo> files)
{
for (int i = 0; i < files.Count; i++)
{
if (ProcessIsSelf(files[i].FullName))
{
// remove self from the list of processes
files.RemoveAt(i);
return;
}
}
}
public static bool ProcessIsSelf(string processPath)
{
string self = VersionTools.SelfLocation;
#if DEBUG
// exclude the vshost file when debugging
if (string.Equals(processPath, self.Substring(0, self.Length - 3) + "vshost.exe", StringComparison.OrdinalIgnoreCase))
return true;
#endif
return string.Equals(processPath, self, StringComparison.OrdinalIgnoreCase);
}
static List<Process> ProcessesNeedClosing(List<FileInfo> baseFiles)
{
List<Process> rProcesses = new List<Process>();
foreach (FileInfo filename in baseFiles)
{
Process[] aProcess = Process.GetProcessesByName(filename.Name.Replace(filename.Extension, ""));
foreach (Process proc in aProcess)
{
try
{
//are one of the exe's in baseDir running?
if (proc.MainModule != null && string.Equals(proc.MainModule.FileName, filename.FullName, StringComparison.OrdinalIgnoreCase))
{
rProcesses.Add(proc);
}
}
catch { }
}
}
return rProcesses;
}
}
}