Skip to content

Commit

Permalink
fix code smells and threading issue
Browse files Browse the repository at this point in the history
  • Loading branch information
bNobo committed Apr 5, 2024
1 parent 3178030 commit 993b33b
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 8 deletions.
21 changes: 14 additions & 7 deletions NeedABreak/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ You should have received a copy of the GNU General Public License
using System.Threading.Tasks;
using System.Timers;
using System.Windows;
using System.Windows.Threading;

namespace NeedABreak
{
Expand Down Expand Up @@ -67,6 +68,7 @@ static App()
//System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo("en");
//System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("en");
ConfigureLog4Net();
_dayStart = DateTime.Today;
}

private static void ConfigureLog4Net()
Expand Down Expand Up @@ -101,7 +103,6 @@ public App()
_updateToolTipTimer.Start();

StartTimer();
_dayStart = DateTime.Today;
Microsoft.Win32.SystemEvents.SessionSwitch += SystemEvents_SessionSwitch;

Logger.Debug("App ctor end");
Expand Down Expand Up @@ -189,12 +190,12 @@ await Current.Dispatcher.Invoke(async () =>
StartTimer();
}

private void StartTimer()
private static void StartTimer()
{
_timer.Start();
}

private void StopTimer()
private static void StopTimer()
{
_timer.Stop();
}
Expand Down Expand Up @@ -228,15 +229,20 @@ public static double GetMinutesLeft()

private static MainWindow GetMainWindow()
{
return Application.Current.MainWindow as MainWindow;
return Current.MainWindow as MainWindow;
}

private void SystemEvents_SessionSwitch(object sender, Microsoft.Win32.SessionSwitchEventArgs e)
private static void SystemEvents_SessionSwitch(object sender, Microsoft.Win32.SessionSwitchEventArgs e)
{
if (e.Reason == Microsoft.Win32.SessionSwitchReason.SessionUnlock)
{
var mainWindow = GetMainWindow();
mainWindow.OnSessionUnlock();
Logger.Debug("SessionUnlock");

Current.Dispatcher.BeginInvoke(() =>
{
var mainWindow = GetMainWindow();
mainWindow.OnSessionUnlock();
});

if (!IsSuspended)
{
Expand All @@ -248,6 +254,7 @@ private void SystemEvents_SessionSwitch(object sender, Microsoft.Win32.SessionSw
}
else if (e.Reason == Microsoft.Win32.SessionSwitchReason.SessionLock)
{
Logger.Debug("SessionLock");
StopTimer();
_updateToolTipTimer.Stop();
_cumulativeScreenTime += DateTime.Now - _startTime;
Expand Down
2 changes: 1 addition & 1 deletion NeedABreak/Extensions/TimeSpanExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public static class TimeSpanExtensions
{
public static string ToHumanFriendlyString(this TimeSpan value)
{
string res = value.ToString();
string res;

if (value < TimeSpan.FromMinutes(1))
{
Expand Down

0 comments on commit 993b33b

Please sign in to comment.