-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTaskTracking.cs
More file actions
122 lines (101 loc) · 3.49 KB
/
TaskTracking.cs
File metadata and controls
122 lines (101 loc) · 3.49 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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace TaskTracking
{
public partial class TaskTracking : Form
{
private System.Timers.Timer timer;
private Stopwatch stopwatch;
private List<string> tasks;
public TaskTracking()
{
InitializeComponent();
this.TopLevel = true;
stopwatch = new Stopwatch();
timer = new System.Timers.Timer();
timer.Interval = 1;
timer.Elapsed += Timer_Elapsed;
var fdr = new StreamReader("tasks.ini");
tasks = new List<string>();
while (!fdr.EndOfStream)
{
tasks.Add(fdr.ReadLine().Trim());
}
fdr.Close();
tasks.Sort();
c_TaskSelection.Items.AddRange(tasks.ToArray());
}
delegate void SetTimerTextCallback(string text);
private void SetTimerText(string text)
{
if (this.l_Time.InvokeRequired)
{
var _dispatcher_ = new SetTimerTextCallback(SetTimerText);
this.Invoke(_dispatcher_, new object[] { text });
}
else
{
this.l_Time.Text = text;
}
}
private void Timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
var timerText = stopwatch.Elapsed.ToString(@"hh\:mm\:ss\.fff");
SetTimerText(timerText);
}
private void b_Start_Click(object sender, EventArgs e)
{
timer.Start();
stopwatch.Reset();
stopwatch.Start();
}
private void b_Stop_Click(object sender, EventArgs e)
{
stopwatch.Stop();
timer.Stop();
var messageTimer = new System.Timers.Timer() { Interval = 15000, AutoReset = false };
messageTimer.Elapsed += MessageTimer_Elapsed;
if (stopwatch.ElapsedMilliseconds == 0) return;
if (c_TaskSelection.SelectedItem == null || c_TaskSelection.SelectedItem.ToString() == string.Empty)
{
l_WriteSuccess.Text = "No data written";
messageTimer.Start();
}
else
{
var elapsedHours = (int)Math.Ceiling((double)stopwatch.ElapsedMilliseconds / 1000 / 60 / 60);
var fdw = new StreamWriter("tasks.csv", true);
fdw.WriteLine($"{DateTime.Today.ToShortDateString()},{c_TaskSelection.SelectedItem.ToString()},{elapsedHours.ToString()}");
l_WriteSuccess.Text = $"{elapsedHours} hours -> {c_TaskSelection.SelectedItem}";
messageTimer.Start();
fdw.Close();
}
}
delegate void ClearMessageTimerCallback();
private void ClearMessageTimer()
{
if (this.l_WriteSuccess.InvokeRequired)
{
var _dispatcher_ = new ClearMessageTimerCallback(ClearMessageTimer);
this.Invoke(_dispatcher_);
}
else
{
this.l_WriteSuccess.Text = string.Empty;
}
}
private void MessageTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
ClearMessageTimer();
}
}
}