-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWeekendOff.cs
More file actions
143 lines (132 loc) · 5.83 KB
/
WeekendOff.cs
File metadata and controls
143 lines (132 loc) · 5.83 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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Threading;
namespace WeekendOff
{
/// <summary>
/// Kill Microsoft Teams or any process written in ProcessToKillList.txt (one process by line) on weekend.
/// Waits for maximum 5 minutes to find the processes and kill them.
/// Ends when all are killed or timed out.
/// </summary>
// ReSharper disable once ClassNeverInstantiated.Global
internal class WeekendOff
{
public static readonly List<string> ProcessToKillOnWeekend = new List<string> {"Teams"};
/// <summary>
/// Stops trying to kill after 2 minutes.
/// </summary>
public static int Timeout = 2 * 60 * 1000;
private static void Main()
{
List<DayOfWeek> workingDays = new List<DayOfWeek> { DayOfWeek.Monday, DayOfWeek.Tuesday, DayOfWeek.Wednesday, DayOfWeek.Thursday, DayOfWeek.Friday };
List<(int, int)> workingHours = new List<(int, int)> { (0, 24), (0, 24), (0, 24), (0, 24), (0, 24) };
try
{
var lines = File.ReadAllLines("WorkingDaysConfig.txt");
workingDays.Clear();
workingDays.AddRange(lines.Select(s => (DayOfWeek)Enum.Parse(typeof(DayOfWeek), s.Split(':')[0])));
workingHours.Clear();
workingHours.AddRange(lines.Select(s =>
{
var xToY = s.Split(':')[1];
var xy = xToY.Split(new[] { "to" }, StringSplitOptions.None);
return (int.Parse(xy[0]), int.Parse(xy[1]));
}));
}
// ReSharper disable once EmptyGeneralCatchClause
catch { }
DayOfWeek day = DateTime.Now.DayOfWeek;
int configDayIndex = workingDays.IndexOf(day);
if (configDayIndex != -1)
{
int dayHour = DateTime.Now.Hour;
var dayWorkingHours = workingHours[configDayIndex];
if (dayHour >= dayWorkingHours.Item1 && dayHour <= dayWorkingHours.Item2)
return;
}
try
{
var lines = File.ReadAllLines("ProcessToKillList.txt");
ProcessToKillOnWeekend.Clear();
ProcessToKillOnWeekend.AddRange(lines);
}
// ReSharper disable once EmptyGeneralCatchClause
catch { }
List<string> toKillList = new List<string> (ProcessToKillOnWeekend);
int timeSpent = 0;
while (timeSpent < Timeout)
{
Thread.Sleep(5);
timeSpent += 5;
var processes = Process.GetProcesses();
foreach (var pToKill in ProcessToKillOnWeekend)
{
foreach (var process in processes)
{
if (process.ProcessName == pToKill)
{
try
{
process.Kill();
toKillList.Remove(pToKill);
}
// ReSharper disable once EmptyGeneralCatchClause
catch
{
toKillList.Remove(pToKill);
}
}
}
}
if (toKillList.Count == 0)
break;
}
RefreshTrayArea();
}
#region "Refresh Notification Area Icons"
[StructLayout(LayoutKind.Sequential)]
public struct RECT
{
public int left;
public int top;
public int right;
public int bottom;
}
[DllImport("user32.dll")]
public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
[DllImport("user32.dll")]
public static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow);
[DllImport("user32.dll")]
public static extern bool GetClientRect(IntPtr hWnd, out RECT lpRect);
[DllImport("user32.dll")]
public static extern IntPtr SendMessage(IntPtr hWnd, uint msg, int wParam, int lParam);
public static void RefreshTrayArea()
{
IntPtr systemTrayContainerHandle = FindWindow("Shell_TrayWnd", null);
IntPtr systemTrayHandle = FindWindowEx(systemTrayContainerHandle, IntPtr.Zero, "TrayNotifyWnd", null);
IntPtr sysPagerHandle = FindWindowEx(systemTrayHandle, IntPtr.Zero, "SysPager", null);
IntPtr notificationAreaHandle = FindWindowEx(sysPagerHandle, IntPtr.Zero, "ToolbarWindow32", "Notification Area");
if (notificationAreaHandle == IntPtr.Zero)
{
notificationAreaHandle = FindWindowEx(sysPagerHandle, IntPtr.Zero, "ToolbarWindow32", "User Promoted Notification Area");
IntPtr notifyIconOverflowWindowHandle = FindWindow("NotifyIconOverflowWindow", null);
IntPtr overflowNotificationAreaHandle = FindWindowEx(notifyIconOverflowWindowHandle, IntPtr.Zero, "ToolbarWindow32", "Overflow Notification Area");
RefreshTrayArea(overflowNotificationAreaHandle);
}
RefreshTrayArea(notificationAreaHandle);
}
private static void RefreshTrayArea(IntPtr windowHandle)
{
const uint wmMousemove = 0x0200;
GetClientRect(windowHandle, out var rect);
for (var x = 0; x < rect.right; x += 5)
for (var y = 0; y < rect.bottom; y += 5)
SendMessage(windowHandle, wmMousemove, 0, (y << 16) + x);
}
#endregion
}
}