Skip to content

Commit

Permalink
Trying to fix issue #5.
Browse files Browse the repository at this point in the history
  • Loading branch information
dlebansais committed Jul 11, 2019
1 parent 7b1efb2 commit 972efa0
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 7 deletions.
45 changes: 42 additions & 3 deletions Kill-Update-Plugin/Kill-Update-Plugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -233,12 +233,14 @@ private void InitServiceManager()
StartType = ServiceStartMode.Manual;

UpdateTimer = new Timer(new TimerCallback(UpdateTimerCallback));
FullRestartTimer = new Timer(new TimerCallback(FullRestartTimerCallback));
UpdateWatch = new Stopwatch();
UpdateWatch.Start();

OnUpdate();

UpdateTimer.Change(CheckInterval, CheckInterval);
UpdateWatch.Start();
FullRestartTimer.Change(FullRestartInterval, Timeout.InfiniteTimeSpan);

Logger.AddLog("InitServiceManager done");
}
Expand All @@ -248,29 +250,64 @@ private void InitServiceManager()

private void StopServiceManager()
{
FullRestartTimer.Change(Timeout.InfiniteTimeSpan, Timeout.InfiniteTimeSpan);
FullRestartTimer = null;
UpdateTimer.Change(Timeout.InfiniteTimeSpan, Timeout.InfiniteTimeSpan);
UpdateTimer = null;
}

private void UpdateTimerCallback(object parameter)
{
// Protection against reentering too many times after a sleep/wake up.
if (UpdateWatch.Elapsed.Seconds < CheckInterval.Seconds / 2)
// There must be at most two pending calls to OnUpdate in the dispatcher.
int NewTimerDispatcherCount = Interlocked.Increment(ref TimerDispatcherCount);
if (NewTimerDispatcherCount > 2)
{
Interlocked.Decrement(ref TimerDispatcherCount);
return;
}

// For debug purpose.
LastTotalElapsed = Math.Round(UpdateWatch.Elapsed.TotalSeconds, 0);

Dispatcher.BeginInvoke(new OnUpdateHandler(OnUpdate));
}

private void FullRestartTimerCallback(object parameter)
{
if (UpdateTimer != null)
{
Logger.AddLog("Restarting the timer");

// Restart the update timer from scratch.
UpdateTimer.Change(Timeout.InfiniteTimeSpan, Timeout.InfiniteTimeSpan);

UpdateTimer = new Timer(new TimerCallback(UpdateTimerCallback));
UpdateTimer.Change(CheckInterval, CheckInterval);

Logger.AddLog("Timer restarted");
}
else
Logger.AddLog("No timer to restart");

FullRestartTimer.Change(FullRestartInterval, Timeout.InfiniteTimeSpan);
Logger.AddLog($"Next check scheduled at {DateTime.UtcNow + FullRestartInterval}");
}

private int TimerDispatcherCount = 1;
private double LastTotalElapsed = double.NaN;

private delegate void OnUpdateHandler();
private void OnUpdate()
{
try
{
Logger.AddLog("%% Running timer callback");

int LastTimerDispatcherCount = Interlocked.Decrement(ref TimerDispatcherCount);
UpdateWatch.Restart();

Logger.AddLog("Watch restarted");
Logger.AddLog($"Watch restarted, Elapsed = {LastTotalElapsed}, pending count = {LastTimerDispatcherCount}");

Settings.RenewKey();

Expand Down Expand Up @@ -361,8 +398,10 @@ private void StopIfRunning(ServiceController Service, bool lockIt)
private static readonly string WindowsUpdateServiceName = "wuauserv";
private static readonly string LockedSettingName = "Locked";
private readonly TimeSpan CheckInterval = TimeSpan.FromSeconds(15);
private readonly TimeSpan FullRestartInterval = TimeSpan.FromHours(1);
private ServiceStartMode? StartType;
private Timer UpdateTimer;
private Timer FullRestartTimer;
private Stopwatch UpdateWatch;
#endregion
}
Expand Down
4 changes: 2 additions & 2 deletions Kill-Update-Plugin/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.64")]
[assembly: AssemblyFileVersion("1.0.0.46")]
[assembly: AssemblyVersion("1.0.0.75")]
[assembly: AssemblyFileVersion("1.0.0.57")]
[assembly: NeutralResourcesLanguage("en-US")]

4 changes: 2 additions & 2 deletions SinglePluginHost/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.64")]
[assembly: AssemblyFileVersion("1.0.0.63")]
[assembly: AssemblyVersion("1.0.0.75")]
[assembly: AssemblyFileVersion("1.0.0.72")]
[assembly: NeutralResourcesLanguage("en-US")]

0 comments on commit 972efa0

Please sign in to comment.