-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathTIAAutosaveForm.cs
335 lines (325 loc) · 14 KB
/
TIAAutosaveForm.cs
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.Remoting.Channels.Ipc;
using System.Threading;
using System.Windows.Forms;
using Siemens.Engineering;
namespace TIAAutoSave
{
public partial class TIAAutosaveForm : Form
{
private TIAAutoSave tIAAutoSave;
private List<TiaPortalProcess> noAutosaveProcesses = new List<TiaPortalProcess>();
private List<TiaPortalProcess> autosaveProcesses = new List<TiaPortalProcess>();
private List<TiaPortalProcess> startedAutosaveProcesses = new List<TiaPortalProcess>();
// do some thread declarations
Thread autoSaveThread;
Thread updateTIAProcessesThread;
public delegate void updateWithAutosaveList();
public updateWithAutosaveList delegateUpdateWithAutosaveList;
public delegate void refreshNoAutosave();
public refreshNoAutosave delegateRefreshNoAutosave;
public delegate void refreshAutosave();
public refreshAutosave delegateRefreshAutosave;
public TIAAutosaveForm()
{
// form work
InitializeComponent();
listViewProcessesWoAS.Columns.Add("Project", 230, HorizontalAlignment.Left);
listViewProcessesWoAS.Columns.Add("PID", 50, HorizontalAlignment.Right);
listViewProcessesWithAS.Columns.Add("Project", 230, HorizontalAlignment.Left);
listViewProcessesWithAS.Columns.Add("PID", 50, HorizontalAlignment.Right);
listViewProcessesWithAS.Columns.Add("Last time saved", 120, HorizontalAlignment.Right);
// thread work
// contruct a new TIAAutoSave Object and start it in its own thread
tIAAutoSave = new TIAAutoSave(this);
tIAAutoSave.SetAutosaveInterval(numericUpDownASTime.Value);
autoSaveThread = new Thread(new ThreadStart(tIAAutoSave.Run));
autoSaveThread.IsBackground = true;
autoSaveThread.Start();
delegateUpdateWithAutosaveList = new updateWithAutosaveList(FillListViewInstWithAS);
// start a thread to update the TIA portal processes
updateTIAProcessesThread = new Thread(new ThreadStart(this.RefreshTIAProcessesProc));
updateTIAProcessesThread.IsBackground = true;
updateTIAProcessesThread.Start();
delegateRefreshNoAutosave = new refreshNoAutosave(RefreshNoAutosaveProcesses);
delegateRefreshAutosave = new refreshAutosave(RefreshAutosaveProcesses);
// settings work
// get the autosave time interval from last time if the file exists
if (File.Exists(AppDomain.CurrentDomain.BaseDirectory + @"\TIAAutosave.ini"))
{
numericUpDownASTime.Value = Decimal.Parse(File.ReadAllText(AppDomain.CurrentDomain.BaseDirectory + @"\TIAAutosave.ini"));
}
else
{
numericUpDownASTime.Value = 5;
}
Thread.Sleep(0);
}
public List<TiaPortalProcess> GetAutosaveProcesses()
{
return autosaveProcesses;
}
public void AddProcToASViaPid(int pid)// add a process to the autosaveProcesses list via the Process.Id
{
// find the process with the given pid in all tiaPortal-processes
IList<TiaPortalProcess> allProcesses = TiaPortal.GetProcesses();
foreach (TiaPortalProcess process in allProcesses)
{
if (process.Id == pid) // if found add ist to the list
{
startedAutosaveProcesses.Add(process);
autosaveProcesses.Add(process);
}
}
this.Invoke(this.delegateUpdateWithAutosaveList); // update view
}
private void RefreshTIAProcessesProc()
{
Thread.Sleep(1000); // thread needs to sleep until windowform is ready
// Process to update the list auf TIA Portal processes every 5 seconds for eternity
while (true)
{
try
{
this.Invoke(this.delegateRefreshNoAutosave);
this.Invoke(this.delegateRefreshAutosave);
Thread.Sleep(5000);
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
}
}
private void RefreshNoAutosaveProcesses()
{
// Get all TIA Portal processes from static method of TiaPortal and add then to the IList noAutosaveProcesses if they are not on the IList autosaveProcesses
// this is to refresh the AutosaveProcesses and to remove a process from this list in case the TIA Portal is closed
IList<TiaPortalProcess> allProcesses = TiaPortal.GetProcesses();
noAutosaveProcesses.Clear();
foreach (var process in allProcesses)
{
if (!(process == null))
{
bool found = false;
foreach (var process2 in autosaveProcesses)
{
if (!(process2 == null))
{
if (process.Id == process2.Id)
{
found = true;
}
}
}
if (!found)
{
noAutosaveProcesses.Add(process);
}
}
}
FillViewBoxInstWoAS();
}
private void RefreshAutosaveProcesses()
{
// Get all TIA Portal processes from static method of TiaPortal and add them to the IList AutosaveProcesses if they are in listViewProcessesWithAS
// this is to refresh the AutosaveProcesses and to remove a process from this list in case the TIA Portal is closed
IList<TiaPortalProcess> allProcesses = TiaPortal.GetProcesses();
autosaveProcesses.Clear();
foreach (var process in allProcesses)
{
if (!(process == null))
{
bool found = false;
bool isStartedAutosaveProcess = false;
foreach (ListViewItem lvi in listViewProcessesWithAS.Items)
{
TiaPortalProcess process2 = (TiaPortalProcess)lvi.Tag;
if (!(lvi.Tag == null))
{
if (process.Id == process2.Id)
{
found = true;
}
}
foreach (TiaPortalProcess process3 in startedAutosaveProcesses)
{
if (process2.Id == process3.Id)
{
isStartedAutosaveProcess = true;
}
}
}
if (found && (process.ProjectPath != null || isStartedAutosaveProcess))
{
autosaveProcesses.Add(process);
}
}
}
FillListViewInstWithAS();
}
// Actionslisteners of the form
private void ButtonRefresh_Click(object sender, EventArgs e)
{
RefreshNoAutosaveProcesses();
RefreshAutosaveProcesses();
}
private void ButtonAddAll_Click(object sender, EventArgs e)
{
List<TiaPortalProcess> removedProcesses = new List<TiaPortalProcess>();
foreach (var process in noAutosaveProcesses)
{
if (!(process == null))
{
autosaveProcesses.Add(process);
removedProcesses.Add(process);
}
}
foreach (var process in removedProcesses)
{
if (!(process == null))
{
noAutosaveProcesses.Remove(process);
}
}
FillListViewInstWithAS();
FillViewBoxInstWoAS();
}
private void ButtonAddSel_Click(object sender, EventArgs e)
{
if (listViewProcessesWoAS.SelectedItems.Count > 0)
{
ListViewItem lvi = listViewProcessesWoAS.SelectedItems[0];
autosaveProcesses.Add((TiaPortalProcess)lvi.Tag);
noAutosaveProcesses.Remove((TiaPortalProcess)lvi.Tag);
}
FillListViewInstWithAS();
FillViewBoxInstWoAS();
}
private void ButtonDelAll_Click(object sender, EventArgs e)
{
List<TiaPortalProcess> removedProcesses = new List<TiaPortalProcess>();
foreach (var process in autosaveProcesses)
{
if (!(process == null))
{
noAutosaveProcesses.Add(process);
removedProcesses.Add(process);
}
}
foreach (var process in removedProcesses)
{
if (!(process == null))
{
autosaveProcesses.Remove(process);
}
}
FillListViewInstWithAS();
FillViewBoxInstWoAS();
}
private void ButtonDelSel_Click(object sender, EventArgs e)
{
if (listViewProcessesWithAS.SelectedItems.Count > 0)
{
ListViewItem lvi = listViewProcessesWithAS.SelectedItems[0];
noAutosaveProcesses.Add((TiaPortalProcess)lvi.Tag);
autosaveProcesses.Remove((TiaPortalProcess)lvi.Tag);
}
FillListViewInstWithAS();
FillViewBoxInstWoAS();
}
private void NumericUpDownASTime_ValueChanged(object sender, EventArgs e) => tIAAutoSave.SetAutosaveInterval(numericUpDownASTime.Value);
// other form work
private void FillViewBoxInstWoAS() // fill the listview for processes without autosave with entries for all processes in the List noAutosaveProcesses
{
listViewProcessesWoAS.Items.Clear();
foreach (var process in noAutosaveProcesses)
{
if (process != null && process.ProjectPath != null)
{
ListViewItem lvi = new ListViewItem();
lvi.Tag = process;
lvi.Text = process.ProjectPath.Name;
ListViewItem.ListViewSubItem lvsi = new ListViewItem.ListViewSubItem();
lvsi.Text = process.Id.ToString();
lvi.SubItems.Add(lvsi);
listViewProcessesWoAS.Items.Add(lvi);
}
}
}
private void FillListViewInstWithAS()// fill the listview for processes with autosave with entries for all processes in the List autosaveProcesses
{
bool isStartedAutosaveProcess = false;
listViewProcessesWithAS.Items.Clear();
foreach (var process in autosaveProcesses)
{
foreach (TiaPortalProcess process2 in startedAutosaveProcesses)
{
if (process.Id == process2.Id)
{
isStartedAutosaveProcess = true;
}
}
if (process != null && process.ProjectPath != null || isStartedAutosaveProcess)
{
ListViewItem lvi = new ListViewItem();
lvi.Tag = process;
if (process.ProjectPath != null)
{
lvi.Text = process.ProjectPath.Name;
}
else
{
lvi.Text = "no project loaded in portal";
}
ListViewItem.ListViewSubItem lvsi = new ListViewItem.ListViewSubItem();
lvsi.Text = process.Id.ToString();
lvi.SubItems.Add(lvsi);
lvsi = new ListViewItem.ListViewSubItem();
if (process.ProjectPath != null)
{
lvsi.Text = process.ProjectPath.LastWriteTime.ToString();
}
else
{
lvsi.Text = "-";
}
lvi.SubItems.Add(lvsi);
listViewProcessesWithAS.Items.Add(lvi);
}
}
}
public void CreateIPCServer()
{
// Create the server channel.
IpcChannel serverChannel =
new IpcChannel("localhost:9090");
// Register the server channel.
System.Runtime.Remoting.Channels.ChannelServices.RegisterChannel(
serverChannel, false);
// Expose an object for remote calls.
System.Runtime.Remoting.RemotingConfiguration.
RegisterWellKnownServiceType(
typeof(TIAAutoSaveServer), "TIAAutoSaveServer.rem",
System.Runtime.Remoting.WellKnownObjectMode.Singleton);
}// Inter process com. to add processes to the list of autosave processes remotely
// Start and exit
private void OnApplicationExit(object sender,
EventArgs e)
{
string createText = numericUpDownASTime.Value.ToString() + Environment.NewLine;
File.WriteAllText(AppDomain.CurrentDomain.BaseDirectory+@"\TIAAutosave.ini", createText);
Environment.Exit(Environment.ExitCode);
}
static void Main(String[] args)
{
TIAAutosaveForm tIAAutosaveForm = new TIAAutosaveForm();
tIAAutosaveForm.CreateIPCServer();
TIAAutoSaveServer.tIAAutosaveForm = tIAAutosaveForm;
Application.ApplicationExit += new EventHandler(tIAAutosaveForm.OnApplicationExit);
Application.Run(tIAAutosaveForm);
}
}
}