-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWeekApplicationContext.cs
126 lines (102 loc) · 2.94 KB
/
WeekApplicationContext.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
#region Using statements
using System.Runtime.Versioning;
using WeekNumberLite2.Properties;
#endregion
namespace WeekNumberLite2
{
internal class WeekApplicationContext : ApplicationContext
{
#region Internal Taskbar GUI
internal IGui? Gui;
#endregion Internal Taskbar GUI
#region Private variables
private readonly System.Windows.Forms.Timer? _timer;
private int _currentWeek;
#endregion Private variables
#region Constructor
[SupportedOSPlatform("windows")]
internal WeekApplicationContext()
{
try
{
Application.ApplicationExit += OnApplicationExit;
_currentWeek = Week.Current();
Gui = new TaskbarGui(_currentWeek);
_timer = GetTimer;
}
catch (Exception ex)
{
_timer?.Stop();
Message.Show(Resources.UnhandledException, ex);
Application.Exit();
}
}
#endregion Constructor
#region Private Timer property
[SupportedOSPlatform("windows")]
private System.Windows.Forms.Timer GetTimer
{
get
{
if (_timer != null)
{
return _timer;
}
System.Windows.Forms.Timer timer = new() { Interval = 10000, Enabled = true };
timer.Tick += OnTimerTick;
return timer;
}
}
#endregion Private Timer property
#region Private event handlers
private void OnApplicationExit(object? sender, EventArgs e)
{
Cleanup(false);
}
[SupportedOSPlatform("windows")]
private void OnTimerTick(object? sender, EventArgs e)
{
UpdateIcon();
}
[SupportedOSPlatform("windows")]
private void UpdateIcon()
{
if (_currentWeek == Week.Current())
{
return;
}
_timer?.Stop();
Application.DoEvents();
try
{
_currentWeek = Week.Current();
Gui?.UpdateIcon(_currentWeek);
}
catch (Exception ex)
{
Message.Show(Resources.FailedToSetIcon, ex);
Cleanup();
throw;
}
if (_timer != null)
{
_timer.Interval = 10000;
_timer.Start();
}
}
#endregion Private event handlers
#region Private methods
private void Cleanup(bool forceExit = true)
{
_timer?.Stop();
_timer?.Dispose();
Gui?.Dispose();
Gui = null;
if (forceExit)
{
Application.Exit();
}
}
#endregion Private methods
}
}