/
StopWatchWrapper.cs
46 lines (38 loc) · 1.49 KB
/
StopWatchWrapper.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
using System;
using System.Diagnostics;
using System.IO;
namespace TrexMinerGUI
{
class StopWatchWrapper
{
public Stopwatch TheStopWatch;
private TimeSpan ElapsedTimeSoFar;
private readonly string FileName;
private readonly System.Threading.Timer TheTimer;
public StopWatchWrapper()
{
FileName = System.IO.Path.GetFileNameWithoutExtension(System.Reflection.Assembly.GetExecutingAssembly().Location) + ".bin";
TheStopWatch = new Stopwatch();
LoadFromFile();
TheTimer = new System.Threading.Timer((e) => SaveToFile(), null, dueTime: TimeSpan.FromSeconds(1), period: TimeSpan.FromMinutes(1));
}
public void SaveToFile()
{
ExternalMethods.WriteAllTextWithBackup(Program.ExecutionPath + FileName, GetTotalElapsedTime().ToString("G"));
}
private void LoadFromFile()
{
if (!File.Exists(Program.ExecutionPath + FileName))
{
ElapsedTimeSoFar = new TimeSpan();
return;
}
string[] TheText = File.ReadAllText(Program.ExecutionPath + FileName).Split(":");
ElapsedTimeSoFar = new TimeSpan(days: int.Parse(TheText[0]), hours: int.Parse(TheText[1]), minutes: int.Parse(TheText[2]), seconds: int.Parse(TheText[3].Substring(0, 2)));
}
public TimeSpan GetTotalElapsedTime()
{
return TheStopWatch.Elapsed.Add(ElapsedTimeSoFar);
}
}
}