Skip to content

Instantly share code, notes, and snippets.

@kleinron
Last active May 12, 2023 16:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kleinron/416e75378ea4577755342035e1726bc5 to your computer and use it in GitHub Desktop.
Save kleinron/416e75378ea4577755342035e1726bc5 to your computer and use it in GitHub Desktop.
static void Main(string[] args)
{
Console.WriteLine("started ClockCounter example");
var clockCounter = new ClockCounter(22, 59, 59);
Console.WriteLine(clockCounter.Time);
clockCounter.IncreaseSecond();
Console.WriteLine(clockCounter.Time);
Console.WriteLine("now let's try again");
clockCounter = new ClockCounter(23, 59, 59);
Console.WriteLine(clockCounter.Time);
clockCounter.IncreaseSecond();
Console.WriteLine(clockCounter.Time);
Console.WriteLine("done, press ENTER to quit");
Console.ReadLine();
}
started ClockCounter example
22:59:59
23:00:00
now let's try again
23:59:59
00:00:00
done, press ENTER to quit
public class ClockCounter
{
private readonly ModCounter seconds;
private readonly ModCounter minutes;
private readonly ModCounter hours;
public ClockCounter() : this(0, 0, 0)
{
}
public ClockCounter(TimeSpan initialTime) :
this(initialTime.Hours, initialTime.Minutes, initialTime.Seconds)
{
}
public ClockCounter(int hours, int minutes, int seconds)
{
this.seconds = new ModCounter(60, seconds);
this.minutes = new ModCounter(60, minutes);
this.hours = new ModCounter(24, hours);
this.seconds.OnZero += () => this.minutes.Increase();
this.minutes.OnZero += () => this.hours.Increase();
}
public void IncreaseSecond()
{
this.seconds.Increase();
}
public TimeSpan Time
{
get
{
return new TimeSpan(hours.Value, minutes.Value, seconds.Value);
}
}
}
using System;
using System.Threading;
namespace Groundhog.ConsoleApp
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("started");
DoSomeLengthyActivity();
Console.WriteLine("done, press ENTER to quit");
Console.ReadLine();
}
private static void DoSomeLengthyActivity()
{
for (int i = 0; i < 100; i++)
{
Thread.Sleep(100);
}
}
}
}
using System;
using System.Threading;
namespace Groundhog.ConsoleApp
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("started");
DoSomeLengthyActivity();
Console.WriteLine("done, press ENTER to quit");
Console.ReadLine();
}
private static void DoSomeLengthyActivity()
{
for (int i = 0; i < 100; i++)
{
Console.WriteLine("still working");
Thread.Sleep(100);
}
}
}
}
using System;
using System.Threading;
namespace Groundhog.ConsoleApp
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("started");
DoSomeLengthyActivity();
Console.WriteLine("done, press ENTER to quit");
Console.ReadLine();
}
private static void DoSomeLengthyActivity()
{
for (int i = 0; i < 100; i++)
{
if (i % 10 == 0)
Console.WriteLine("still working");
Thread.Sleep(100);
}
}
}
}
static void Main(string[] args)
{
Console.WriteLine("ModCounter example");
ModCounter myCounter = new ModCounter(3);
Console.WriteLine(myCounter.Value); // value is 0
myCounter.Increase();
Console.WriteLine(myCounter.Value); // value is 1
myCounter.Increase();
Console.WriteLine(myCounter.Value); // value is 2
myCounter.Increase();
Console.WriteLine(myCounter.Value); // value is 0
Console.WriteLine(myCounter.IsZero); // True
Console.WriteLine("done, press ENTER to quit");
Console.ReadLine();
}
public class HealthMonitor
{
public int MaxConsequtiveFailures { get; private set; }
private readonly ModCounter modCounter;
public HealthMonitor(int maxConsequtiveFailures) :
this(new ModCounter(maxConsequtiveFailures))
{
MaxConsequtiveFailures = maxConsequtiveFailures;
}
private HealthMonitor(ModCounter modCounter)
{
this.modCounter = modCounter;
this.modCounter.OnZero += InvokeReachedMaxConsequentFailures;
}
public void Pass()
{
modCounter.Reset();
}
public void Fail()
{
modCounter.Increase();
}
public event Action ReachedMaxConsequentFailures;
private void InvokeReachedMaxConsequentFailures()
{
Action failures = ReachedMaxConsequentFailures;
if (failures != null) failures();
}
}
public class Global : System.Web.HttpApplication
{
public static HealthMonitor HealthMonitor;
protected void Application_Start(object sender, EventArgs e)
{
HealthMonitor = new HealthMonitor(3);
HealthMonitor.ReachedMaxConsequentFailures += SendMailToAppAdmin;
}
void SendMailToAppAdmin()
{
// code to send a warning mail
// to the application administrator
}
// .. code ..
}
public class MyService : System.Web.Services.WebService
{
[WebMethod]
public void DoSomething()
{
bool allOk = false;
// do a lot of stuff,
// set allOk to false if needed
// ...
if (allOk)
Global.HealthMonitor.Pass();
else
Global.HealthMonitor.Fail();
}
}
private static void DoSomeLengthyActivity()
{
var myCounter = new ModCounter(10);
for (int i = 0; i < 100; i++)
{
myCounter.Increase();
if (myCounter.IsZero)
Console.WriteLine("still working");
Thread.Sleep(100);
}
}
private static void DoSomeLengthyActivity()
{
var myCounter = new ModCounter(10);
myCounter.OnZero += () => Console.WriteLine("still working");
for (int i = 0; i < 100; i++)
{
myCounter.Increase();
Thread.Sleep(100);
}
}
public class ModCounter
{
private readonly int modBase;
private int theValue;
public ModCounter(int modBase) : this (modBase, 0)
{
}
public ModCounter(int modBase, int initialValue)
{
if (modBase <= 1)
throw new ArgumentOutOfRangeException("modBase", "value must be greater than 1");
this.modBase = modBase;
theValue = initialValue % modBase;
}
public int Increase()
{
var increased = theValue + 1;
if (increased == modBase)
increased = 0;
theValue = increased;
return increased;
}
public bool IsZero
{
get
{
return theValue == 0;
}
}
public int Value
{
get
{
return theValue;
}
}
}
public class ModCounter
{
public event Action OnZero;
private void InvokeOnZero()
{
Action action = OnZero;
if (action != null) action();
}
// .. code ..
public int Increase()
{
var increased = theValue + 1;
var shouldResetToZero = increased == modBase;
if (shouldResetToZero)
{
theValue = 0;
InvokeOnZero();
}
else
theValue = increased;
return theValue;
}
}
ModCounter example
0
1
2
0
True
done, press ENTER to quit
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment