This repository was archived by the owner on Dec 5, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProcessController.cs
More file actions
190 lines (150 loc) · 5.65 KB
/
ProcessController.cs
File metadata and controls
190 lines (150 loc) · 5.65 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
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading;
using System.Windows.Forms;
using MultiAppLauncher.Properties;
namespace MultiAppLauncher
{
public class ProcessController
{
public EventHandler<EventArgs> AllProcessesStarted;
private readonly IMainFormView _view;
private readonly ITaskbarList _taskbarList;
private readonly Thread _startProcessThread;
private bool _stopping;
private readonly AutoResetEvent _startProcessingEvent;
public ProcessController(IMainFormView view)
{
Application.ApplicationExit += ApplicationOnApplicationExit;
_taskbarList = (ITaskbarList)new CoTaskbarList();
_taskbarList.HrInit();
_view = view;
_startProcessingEvent = new AutoResetEvent(false);
_startProcessThread = new Thread(StartProcessWorker);
_startProcessThread.Start();
}
private void ApplicationOnApplicationExit(object sender, EventArgs eventArgs)
{
_stopping = true;
_startProcessingEvent.Set();
_startProcessThread.Join();
}
public void KillAll()
{
foreach (ListViewItem lvi in _view.GetListViewItems())
{
var holder = lvi.GetProcessHolder();
if (holder == null)
continue;
try
{
holder.Process.Kill();
}
catch (Exception ex)
{
MessageBox.Show(_view, String.Format(
"Failed to kill {0}. {1}", holder.Process.ProcessName, ex));
}
}
}
public void BringSelectedAppToFront()
{
var selectedItem = _view.GetSelectedItems().FirstOrDefault();
if (selectedItem == null)
return;
var p = selectedItem.GetProcessHolder();
if (p == null)
return;
Unmanaged.SetForegroundWindow(p.Process.MainWindowHandle.ToInt32());
}
private IEnumerable<ListViewItem> GetItemsToExecute()
{
var items = _view.GetSelectedItems();
if (!items.Any())
items = _view.GetListViewItems();
return items;
}
private void StartProcessWorker()
{
while (true)
{
_startProcessingEvent.WaitOne();
if (_stopping)
break;
foreach (var lvi in GetItemsToExecute().Where(t => t.Tag == null))
{
var holder = new ProcessHolder
{
Process = new Process
{
StartInfo =
{
FileName = lvi.Text,
UseShellExecute = true,
Arguments = lvi.SubItems[Columns.Profile].Text
},
EnableRaisingEvents = true
}
};
_view.SetToolStripProgressBar(0);
holder.Process.Exited += ProcessOnExited;
holder.Process.Start();
WaitForMainWindow(holder.Process);
ModifyProcess(holder.Process.MainWindowHandle);
holder.CpuUsage = holder.Process.TotalProcessorTime.TotalMilliseconds;
lvi.Tag = holder;
_view.SetListViewItem(lvi, Columns.Status, Resources.StatusStarting);
double totalMs = _view.SoftStartSeconds * 1000d;
TimeSpan wait = TimeSpan.FromMilliseconds(totalMs / 10d);
for (int i = 0; i <= 100; i += 10)
{
_view.SetToolStripProgressBar(i);
Thread.Sleep(wait);
}
_view.SetListViewItem(lvi, Columns.Status, Resources.StatusRunning);
_view.SetToolStripProgressBar(0);
}
var subscribers = AllProcessesStarted;
if (subscribers != null)
subscribers(this, new EventArgs());
}
}
public void StartProcesses()
{
_startProcessingEvent.Set();
}
private static void WaitForMainWindow(Process process)
{
while (process.MainWindowHandle == IntPtr.Zero && !process.HasExited)
Thread.Sleep(10);
}
private void ModifyProcess(IntPtr hWnd)
{
_taskbarList.DeleteTab(hWnd);
}
private void ProcessOnExited(object sender, EventArgs eventArgs)
{
foreach (var item in _view.GetListViewItems())
{
var process = (ProcessHolder)item.Tag;
if (process == null)
{
_view.SetListViewItem(item, Columns.Status, String.Empty);
_view.SetListViewItem(item, Columns.Cpu, String.Empty);
}
else if (process.Process.HasExited)
{
_view.SetListViewItem(item, Columns.Status, String.Empty);
_view.SetListViewItem(item, Columns.Cpu, String.Empty);
item.Tag = null;
}
else
{
_view.SetListViewItem(item, Columns.Status, Resources.StatusRunning);
}
}
}
}
}