-
Notifications
You must be signed in to change notification settings - Fork 1
/
TIAAutoSave.cs
75 lines (71 loc) · 2.58 KB
/
TIAAutoSave.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
using System;
using System.Collections.Generic;
using System.Linq;
using Siemens.Engineering;
namespace TIAAutoSave
{
public class TIAAutoSave
{
private decimal autoSaveInterval; //Interval of autosaves in minutes
private object myConstructor; // constructing object of this object
public TIAAutoSave(object constructor)
{
this.myConstructor = constructor;
}
public void Run()
{
Console.WriteLine("TIAAutoSave: Thread started");
//Endlessly run this instruction
while (true)
{
try
{
Console.WriteLine("TIAAutoSave: Started main while");
//Wait
Console.WriteLine("TIAAutoSave: going to sleep for " + autoSaveInterval + "minutes.");
System.Threading.Thread.Sleep((int)(autoSaveInterval * 1000 * 60));
Console.WriteLine("TIAAutoSave: Woke up");
//Call function to save portal projects
SavePortals();
}
catch (Exception)
{
}
}
}
public decimal GetAutoSaveInterval()
{
return this.autoSaveInterval;
}
public void SetAutosaveInterval(decimal interval) // interval in minutes
{
this.autoSaveInterval = interval;
}
void SavePortals()
{
TIAAutosaveForm tIAAutosaveForm = (TIAAutosaveForm)myConstructor;
IList<TiaPortalProcess> processes = tIAAutosaveForm.GetAutosaveProcesses();
TiaPortal MyTiaPortal;
//Loop through all TIA portal processes and attempt to save
foreach (var process in processes)
{
try
{
DateTime localDate = DateTime.Now;
MyTiaPortal = process.Attach();
if (MyTiaPortal.Projects.FirstOrDefault() != null)
{
MyTiaPortal.Projects.FirstOrDefault().Save();
Console.WriteLine(MyTiaPortal.Projects.FirstOrDefault().Name + " saved: " + localDate.ToString());
}
}
catch (Exception e)
{
//Print any error messages to the console
Console.WriteLine(e.Message);
}
}
tIAAutosaveForm.Invoke(tIAAutosaveForm.delegateUpdateWithAutosaveList); // update the form
}
}
}