public class AuctionTimer : IDisposable
{
public static readonly ConcurrentDictionary<string, AuctionTimer> Timers;
private readonly Timer timer;
static AuctionTimer()
{
Timers = new ConcurrentDictionary<string, AuctionTimer>();
}
private AuctionTimer(string AuctionID, int ExecutionTime)
{
this._AuctionID = AuctionID;
timer = new Timer();
TimeCount = ExecutionTime;
timer.Interval = new TimerUtil().getAuctionInterval();
timer.Elapsed += (s, e) => MonitorElapsedTime();
timer.Start();
}
private int TimeCount { get; set; }
private string _AuctionID { get; set; }
public static void StartTimer(string AuctionID, int ExecutionTime)
{
var newTimer = new AuctionTimer(AuctionID, ExecutionTime);
if (!Timers.TryAdd(AuctionID, newTimer))
{
newTimer.Dispose();
}
}
public static void StopTimer(string AuctionID)
{
AuctionTimer oldTimer;
if (Timers.TryRemove(AuctionID, out oldTimer))
{
oldTimer.Dispose();
}
}
public void Dispose()
{
// Stop might not be necessary since we call Dispose
timer.Stop();
timer.Dispose();
}
private void MonitorElapsedTime()
{
if (TimeCount == new TimerUtil().getAuctionInterval())
{
StopTimer(_AuctionID);
//do the other process in db
}
else
{
TimeCount++;
}
}
}
Following is the code for my Auction Timer what i am trying to do here is making an auction storing starting and ending date so lets say an auction is going to last for 5 hours so the end date will be 5 hours from current time i am starting the Auction timer right after storing the end date for the auction it is working as expected but the problem i am facing now is the timer is bit delay from the end time around 2,3 minutes roughly that's means the process which is suppose to run immediately after the auction expires is running 2,3 minutes late i am not really sure what i am missing here.
I was able to solve this problem using this framework.
Related
I am currently developing a multiplayer web game with timer using ASP.NET Core. For real time communication I am using SignalR. Everythning up to the moment had been working just fine.
The way I implemented the timer functionality was the following: I created a static dictionary where the key is the Id of the game and the value is the timer corresponding to the given game. Then I exposed public methods for managing the state of the dictionary:
public class TimerManager
{
private static readonly Dictionary<string, GameTimer> gameTimersByGameIds = new();
public void AttachTimerToGameState(GameTimer timer, string gameId)
{
if (!gameTimersByGameIds.ContainsKey(gameId))
{
gameTimersByGameIds.Add(gameId, timer);
return;
}
gameTimersByGameIds[gameId] = timer;
}
public GameTimer? GetTimer(string gameId)
{
if (!gameTimersByGameIds.ContainsKey(gameId))
{
return null;
}
return gameTimersByGameIds[gameId];
}
public GameTimer CreateTimer(GameState gameState)
{
if (gameState.RemainingSecondsByUserNames.Count == 0)
{
return ActivatorUtilities.CreateInstance<StandardTimer>(_serviceProvider, gameState);
}
else
{
return ActivatorUtilities.CreateInstance<ChessTimer>(_serviceProvider, gameState, _gamesService);
}
}
}
I created my own base GameTimer class which encapsulates a System.Timers.Timer inside of it:
public abstract class GameTimer
{
protected readonly System.Timers.Timer _timer = new();
public virtual void Reset()
{
_timer.AutoReset = true;
_timer.Interval = 1_000;
}
public virtual void Start()
{
_timer.Start();
}
public virtual void Dispose()
{
_timer.Dispose();
}
}
Then I inherited from the abstract GameTimer class to create different types of timers.
public class StandardTimer : GameTimer
public class ChessTimer : GameTimer
The problem is inside of the ChessTimer class:
public class ChessTimer : GameTimer
{
private readonly GameState _gameState;
private readonly IGameService _gameService;
private readonly IHubContext<GameHub, IGameClient> _hubContext;
private readonly IMatchmakingService _matchmakingService;
private readonly IGamesService _gamesService;
public ChessTimer(
GameState gameState,
IGamesService gamesService,
IGameService gameService,
IHubContext<GameHub, IGameClient> hubContext,
IMatchmakingService matchmakingService)
{
_gameState = gameState;
_gameService = gameService;
_hubContext = hubContext;
_matchmakingService = matchmakingService;
_gamesService = gamesService;
Reset();
_timer.Elapsed += async (sender, args) => await OnTimedEvent(sender, args);
}
public int SecondsRemaining { get; private set; }
public override void Reset()
{
string currentPlayerName = _gameState.CurrentTeam.CurrentPlayer.UserName;
SecondsRemaining = _gameState.RemainingSecondsByUserNames[currentPlayerName];
base.Reset();
}
private async Task OnTimedEvent(object? sender, ElapsedEventArgs args)
{
if (SecondsRemaining >= 0)
{
string currentPlayerUserName = _gameState.CurrentTeam.CurrentPlayer.UserName;
_gameState.RemainingSecondsByUserNames[currentPlayerUserName] = SecondsRemaining;
int minutes = SecondsRemaining / 60;
int seconds = SecondsRemaining % 60;
var viewModel = new UpdateGameTimerViewModel
{
Minutes = minutes,
Seconds = seconds,
};
foreach (Player player in _gameState.Players)
{
if (player.ConnectionId == null)
{
continue;
}
await _hubContext.Clients
.Client(player.ConnectionId)
.UpdateGameTimer(viewModel);
}
SecondsRemaining--;
return;
}
if (_gameState.RemainingSecondsByUserNames.All(x => x.Value <= 0))
{
_gameState.EndGame();
}
else
{
_gameState.NextTeam();
}
foreach (Player player in _gameState.Players)
{
_gameService.FillPlayerTiles(_gameState, player);
}
foreach (Player player in _gameState.Players)
{
if (player.ConnectionId == null)
{
continue;
}
var viewModel = _gameService.MapFromGameState(_gameState, player.UserName);
await _hubContext.Clients
.Client(player.ConnectionId)
.UpdateGameState(viewModel);
if (_gameState.IsGameOver)
{
_matchmakingService.RemoveUserFromGame(player.UserName);
await _hubContext.Groups
.RemoveFromGroupAsync(
player.ConnectionId, _gameState.GameId);
}
}
if (_gameState.IsGameOver)
{
_matchmakingService.RemoveGameState(_gameState.GameId);
await _gamesService!.SaveGameAsync(new SaveGameInputModel
{
GameId = _gameState.GameId,
Players = _gameState.Players
});
Dispose();
}
Reset();
}
}
Basically the way my OnTimedEvent method works is that it executes every second. Then checks whether the time of all players has run out. If it hasn't it updates the state of the game, otherwise is ends the game. After everything else is done, I try to save the game inside my database in order to enable the users to see summary about their played games. I am using a class called GamesService for this task. It uses UserManager internally to perform some work with the Users in the database.
if (_gameState.IsGameOver)
{
_matchmakingService.RemoveGameState(_gameState.GameId);
await _gamesService!.SaveGameAsync(new SaveGameInputModel
{
GameId = _gameState.GameId,
Players = _gameState.Players
});
Dispose();
}
Reset();
The problem is that when the game ends and the above code is executed it produces the following exception:
Unhandled exception. Unhandled exception. System.ObjectDisposedException: Cannot access a disposed object.
Object name: 'UserManager`1'.
at Microsoft.AspNetCore.Identity.UserManager`1.ThrowIfDisposed()
at Microsoft.AspNetCore.Identity.UserManager`1.FindByNameAsync(String userName)
at SuperScrabble.Services.Data.Users.UsersService.GetByUserNameAsync(String userName) in C:\Users\georg\Source\Github\SuperScrabble\src\Server\Services\SuperScrabble.Services.Data\Users\UsersService.cs:line 64
at SuperScrabble.Services.Data.Games.GamesService.SaveGameAsync(SaveGameInputModel input) in C:\Users\georg\Source\Github\SuperScrabble\src\Server\Services\SuperScrabble.Services.Data\Games\GamesService.cs:line 52
at SuperScrabble.WebApi.Timers.ChessTimer.OnTimedEvent(Object sender, ElapsedEventArgs args) in C:\Users\georg\Source\Github\SuperScrabble\src\Server\WebApi\SuperScrabble.WebApi\Timers\ChessTimer.cs:line 120
at SuperScrabble.WebApi.Timers.ChessTimer.<.ctor>b__5_0(Object sender, ElapsedEventArgs args) in C:\Users\georg\Source\Github\SuperScrabble\src\Server\WebApi\SuperScrabble.WebApi\Timers\ChessTimer.cs:line 35
at System.Threading.Tasks.Task.<>c.<ThrowAsync>b__128_1(Object state)
at System.Threading.QueueUserWorkItemCallback.<>c.<.cctor>b__6_0(QueueUserWorkItemCallback quwi)
at System.Threading.ExecutionContext.RunForThreadPoolUnsafe[TState](ExecutionContext executionContext, Action`1 callback, TState& state)
at System.Threading.QueueUserWorkItemCallback.Execute()
at System.Threading.ThreadPoolWorkQueue.Dispatch()
at System.Threading.PortableThreadPool.WorkerThread.WorkerThreadStart()
at System.Threading.Thread.StartCallback()
It seems to me that the _gamesService object which I pass to the CreateInstance() method inside the TimerManager.CreateTimer() method is already disposed or that some of the services it uses internally are.
public GameTimer CreateTimer(GameState gameState)
{
if (gameState.RemainingSecondsByUserNames.Count == 0)
{
return ActivatorUtilities.CreateInstance<StandardTimer>(_serviceProvider, gameState);
}
else
{
return ActivatorUtilities.CreateInstance<ChessTimer>(_serviceProvider, gameState, _gamesService);
}
}
I am not sure if what I am trying to do is correct. I need to use service classes inside my timer classes to perform operations every time the timer ticks. However, I cannot use dependency injection and that's the reason why I use the ActivatorUtilities class to instantiate the object.
I create all timers from a method inside my SignalR hub class:
private async Task StartGameAsync()
{
var gameState = _matchmakingService.GetGameState(UserName);
string gameId = gameState.GameId;
foreach (Player player in gameState.Players)
{
await Groups.AddToGroupAsync(player.ConnectionId!, gameId);
}
var timer = _timerManager.CreateTimer(gameState);
Console.WriteLine(timer.GetType().Name);
_timerManager.AttachTimerToGameState(timer, gameId);
await Clients.Group(gameId).StartGame(gameId);
await UpdateGameStateAsync(gameState);
timer.Start();
}
So my question would be: Is it possible to find a work around of the problem?
If you need any more clarification please feel free to ask me anything. Any help would be greatly appreciated! Thanks
I am making a console application that will later be a Windows service application through Topshelf.
I am struggling with timers, I setup a timer with autoreset of 20 seconds. When I start the timer it works. And when I stop it it stops.
Now to the problem, I want to keep the value when stopping it.
I need an event handler as Timer offers. But I also need to have a way that remembers the time as Stopwatch does. Which should I choose? I have access to SQL server and save it.
Lets say I let it run for 10 seconds since autoreset is true, when I stop it. And then wait, and then start the timer again.
In my opinion, the next time I start the timer it should only run for 10 seconds. So the total time that fires up the event is 20 second and not 30.
Is there any way to "save" elapsed time on it?
public class MySampleClient
{
System.Timers.Timer BatchTimer;
public MySampleClient()
{
BatchTimer = new System.Timers.Timer(BatchSampleTimer) { AutoReset = true };
}
public async Task startSession()
{
BatchTimer.Elapsed += BatchTimerVoid;
}
public void BatchIDTrigger(BatchTagDataModel btdm)
{
string btdmValue = btdm.Value.ToLower();
if (batchNumberTimerControllerList.Contains(btdm.DisplayName))
{
if (btdmValue == "true")
{
BatchTimer.Start();
}
else
{
BatchTimer.Stop();
}
}
}
public void BatchTimerVoid(Object source, System.Timers.ElapsedEventArgs e)
{
// Something happens here every 20 seconds.
}
}
I have written a simple extension to a System.Timers.Timer to provide you with a Pause() method. It uses a backing System.Diagnostics.Stopwatch to modify the Timer.Interval according to the elapsed period before the pause.
public class ExtendedTimer: System.Timers.Timer
{
public long MillisecondsElapsed { get { return backingStopwatch.ElapsedMilliseconds; } }
public new double Interval { get; private set; }
private Stopwatch backingStopwatch;
public ExtendedTimer(double interval) : base(interval)
{
Interval = interval;
backingStopwatch = new Stopwatch();
base.Elapsed += ExtendedTimer_Elapsed;
}
private void ExtendedTimer_Elapsed(object sender, ElapsedEventArgs e)
{
base.Interval = this.Interval;
backingStopwatch.Restart();
}
public new void Start()
{
base.Interval = this.Interval - backingStopwatch.ElapsedMilliseconds;
base.Start();
backingStopwatch.Start();
}
public new void Stop()
{
base.Stop();
backingStopwatch.Reset();
}
public void Pause()
{
base.Stop();
backingStopwatch.Stop();
}
}
Since there is now some overhead introduced, I cannot speak to how accurately it will maintain precision timings compared to the original implementation.
Let me first confess that I am a fairly green programmer but I am in dire straits trying to figure out what is wrong with my application.
The goal so far is to make a timer kick off when the button is clicked and the elapsed time continually display on the text box.
There are probably better ways to implement this but humor me for a second and I practice creating events and using them in programs.
What I see happening when I launch the code is that it just freezes and never recovers, I need to end the app with the task manager.
Any pointers on what I may be doing wrong and how to fix it will be appreciated.
// see clock class below containing delegate and event instantiation
public class Clock
{
public delegate void TimeChangedHandler(object clock, TimeEventArgs timeInfo);
public TimeChangedHandler TimeChanged;
public void RunClock()
{
TimeEventArgs e = new TimeEventArgs();//initialize args
while (e.keepCounting)
{
Thread.Sleep(1000);
e.EndTime = DateTime.Now;
if (e.StartTime != e.EndTime)
{
e.duration = e.EndTime.Subtract(e.StartTime);
}
if (TimeChanged != null)
{
TimeChanged(this, e);
}
}
}
//see timeevent args description below:
public class TimeEventArgs : EventArgs
{
public TimeSpan duration;
public DateTime StartTime { get; set; }
public DateTime EndTime { get; set; }
public bool keepCounting = false;
public TimeEventArgs()
{
StartTime = DateTime.Now;
EndTime = DateTime.Now;
keepCounting = true;
}
}
//See form class below:
public partial class TimeApp : Form
{
public TimeApp()
{
InitializeComponent();
}
private void startStopButton_Click(object sender, EventArgs e)
{
var theClock = new Clock();
var timeApp = new TimeApp();
timeApp.Subscribe(theClock);
theClock.RunClock();
}
public void Subscribe(Clock theClock)
{
theClock.TimeChanged += new Clock.TimeChangedHandler(NewTime);
}
public void NewTime(object theClock, TimeEventArgs e)
{
displayBox.Text = e.duration.Hours.ToString() + ":"
+ e.duration.Minutes.ToString() + ":" + e.duration.Seconds.ToString();
}
}
Your RunClock method blocks the UI (because of the Thread.Sleep(1000); call), which makes it impossible to stop.
Instead of looping, you should look at adding a Windows.Forms.Timer to your form, and using it to drive the clock.
You are suspending your main (UI) thread when calling Thread.Sleep(1000) - which is why your app is non-responsive.
Use a Timer (instead of Thread.Sleep()) and spin off any processing/long running code to a BackgroundWorker with for any processing you need to do. That way, your UI will stay responsive.
I have a maze game and I'm trying to create two Timers at a time.
1st (Exits the game after 300 secs)
t1.Interval = 30000;
t1.Enabled = true;
t1.Elapsed += new ElapsedEventHandler(hiddenTimer);
public static void hiddenTimer(object source, ElapsedEventArgs e)
{
Console.Clear();
Environment.Exit(1);
}
2nd (Displays the time remaining every 1 sec (like a real timer))
t2.Interval = 1000;
t2.Enabled = true;
t2.Elapsed += new ElapsedEventHandler(showTimer);
public static void showTimer(object source, ElapsedEventArgs e)
{
Console.Write(timeLeft);
}
I would want to pass declare timeLeft globally but it says that "An object reference is required for the non-static field, method, or property..."
How would I declare it properly?
By making a static property:
public static Double TimeLeft { get; set; }
This is if you want to Publicliy accessable from your entire context, if you want it private, just change public to private.
Just a side note, the built in Timer doesn't support polling for the remaining time until the next elapse. Either you decrease TimeLeft in each Elapse-event on the 1sec timer or you can have a look at this.
Edit
Here is one way to do it with one timer, first I declare two properties and one constant field that I use, don't bother that they are static, it's just easier to run it as a console application this way.
public static Timer SystemTimer { get; set; }
public static double Elapsed { get; set; }
private const double CycleInterval = 1000;
Then in my Main-method I have the following to initiate my Timer
SystemTimer = new Timer();
SystemTimer.Interval = CycleInterval;
SystemTimer.Enabled = true;
SystemTimer.Elapsed += Cycle;
SystemTimer.Start();
Having this, the Cycle-event handler can look like this:
static void Cycle(object sender, ElapsedEventArgs e)
{
Elapsed += CycleInterval;
if ((Elapsed%5000) == 0.0)
{
Console.WriteLine("5 sec elapsed!");
// Do stuff each 5 sec
}
if ((Elapsed % 10000) == 0.0)
{
Console.WriteLine("10 sec elapsed!");
// Do stuff each 10 sec
}
Console.WriteLine("Elapsed: {0}", Elapsed);
}
You could also have Elapsed being a TimeSpan, but you can refactor this as you like.
Here's my complete source code that I used:
using System;
using System.IO;
using System.Timers;
namespace ConsoleApplication5
{
class Program
{
public static Timer SystemTimer { get; set; }
public static double Elapsed { get; set; }
private const double CycleInterval = 1000;
static void Main(string[] args)
{
SystemTimer = new Timer();
SystemTimer.Interval = CycleInterval;
SystemTimer.Enabled = true;
SystemTimer.Elapsed += Cycle;
SystemTimer.Start();
while (true) ;
}
static void Cycle(object sender, ElapsedEventArgs e)
{
Elapsed += CycleInterval;
if ((Elapsed%5000) == 0.0)
{
Console.WriteLine("5 sec elapsed!");
// Do stuff each 5 sec
}
if ((Elapsed % 10000) == 0.0)
{
Console.WriteLine("10 sec elapsed!");
// Do stuff each 10 sec
}
Console.WriteLine("Elapsed: {0}", Elapsed);
}
}
}
And this is what it looks like when I run it:
First of all, you should declare your timeLeft as a static if you want it to behave like a global variable.
Secondly I'd use one timer and keep track of the time separately for each event:
static DateTime startTime = DateTime.Now;
static DateTime lastTime = DateTime.Now;
In your timer, which should be set to something to give more accuracy like 1/10 of a second, do this:
if (DateTime.Now - lastTime > new TimeSpan(0, 0, 1))
// Update the time here for your 1s clock
lastTime = DateTime.Now;
if (DateTime.Now - startTime > new TimeSpan(0, 0, 300))
// Exit the game
Your timings will be more accurate this way.
Mark it static:
public static int TimeLeft;
Your timeLeft memeber is not static.
Make it static or make showTimer method non static.
Regards.
I'm not really writing an alarm clock application, but it will help to illustrate my question.
Let's say that I have a method in my application, and I want this method to be called every hour on the hour (e.g. at 7:00 PM, 8:00 PM, 9:00 PM etc.). I could create a Timer and set its Interval to 3600000, but eventually this would drift out of sync with the system clock. Or I could use a while() loop with Thread.Sleep(n) to periodically check the system time and call the method when the desired time is reached, but I don't like this either (Thread.Sleep(n) is a big code smell for me).
What I'm looking for is some method in .Net that lets me pass in a future DateTime object and a method delegate or event handler, but I haven't been able to find any such thing. I suspect there's a method in the Win32 API that does this, but I haven't been able to find that, either.
Or, you could create a timer with an interval of 1 second and check the current time every second until the event time is reached, if so, you raise your event.
You can make a simple wrapper for that :
public class AlarmClock
{
public AlarmClock(DateTime alarmTime)
{
this.alarmTime = alarmTime;
timer = new Timer();
timer.Elapsed += timer_Elapsed;
timer.Interval = 1000;
timer.Start();
enabled = true;
}
void timer_Elapsed(object sender, ElapsedEventArgs e)
{
if(enabled && DateTime.Now > alarmTime)
{
enabled = false;
OnAlarm();
timer.Stop();
}
}
protected virtual void OnAlarm()
{
if(alarmEvent != null)
alarmEvent(this, EventArgs.Empty);
}
public event EventHandler Alarm
{
add { alarmEvent += value; }
remove { alarmEvent -= value; }
}
private EventHandler alarmEvent;
private Timer timer;
private DateTime alarmTime;
private bool enabled;
}
Usage:
AlarmClock clock = new AlarmClock(someFutureTime);
clock.Alarm += (sender, e) => MessageBox.Show("Wake up!");
Please note the code above is very sketchy and not thread safe.
Interesting, I've actually come across a very similar issue and went looking for a method in the .Net framework that would handle this scenario. In the end, we ended up implementing our own solution that was a variation on a while loop w/ Thread.Sleep(n) where n gets smaller the closer you get to the desired target time (logarithmically actually, but with some reasonable thresholds so you're not maxing the cpu when you get close to the target time.) Here's a really simple implementation that just sleeps half the time between now and the target time.
class Program
{
static void Main(string[] args)
{
SleepToTarget Temp = new SleepToTarget(DateTime.Now.AddSeconds(30),Done);
Temp.Start();
Console.ReadLine();
}
static void Done()
{
Console.WriteLine("Done");
}
}
class SleepToTarget
{
private DateTime TargetTime;
private Action MyAction;
private const int MinSleepMilliseconds = 250;
public SleepToTarget(DateTime TargetTime,Action MyAction)
{
this.TargetTime = TargetTime;
this.MyAction = MyAction;
}
public void Start()
{
new Thread(new ThreadStart(ProcessTimer)).Start();
}
private void ProcessTimer()
{
DateTime Now = DateTime.Now;
while (Now < TargetTime)
{
int SleepMilliseconds = (int) Math.Round((TargetTime - Now).TotalMilliseconds / 2);
Console.WriteLine(SleepMilliseconds);
Thread.Sleep(SleepMilliseconds > MinSleepMilliseconds ? SleepMilliseconds : MinSleepMilliseconds);
Now = DateTime.Now;
}
MyAction();
}
}
You could simply reset the timer duration each time it fires, like this:
// using System.Timers;
private void myMethod()
{
var timer = new Timer {
AutoReset = false, Interval = getMillisecondsToNextAlarm() };
timer.Elapsed += (src, args) =>
{
// Do timer handling here.
timer.Interval = getMillisecondsToNextAlarm();
timer.Start();
};
timer.Start();
}
private double getMillisecondsToNextAlarm()
{
// This is an example of making the alarm go off at every "o'clock"
var now = DateTime.Now;
var inOneHour = now.AddHours(1.0);
var roundedNextHour = new DateTime(
inOneHour.Year, inOneHour.Month, inOneHour.Day, inOneHour.Hour, 0, 0);
return (roundedNextHour - now).TotalMilliseconds;
}
You could create an Alarm class which has a dedicated thread which goes to sleep until the specified time, but this will use the Thread.Sleep method. Something like:
/// <summary>
/// Alarm Class
/// </summary>
public class Alarm
{
private TimeSpan wakeupTime;
public Alarm(TimeSpan WakeUpTime)
{
this.wakeupTime = WakeUpTime;
System.Threading.Thread t = new System.Threading.Thread(TimerThread) { IsBackground = true, Name = "Alarm" };
t.Start();
}
/// <summary>
/// Alarm Event
/// </summary>
public event EventHandler AlarmEvent = delegate { };
private void TimerThread()
{
DateTime nextWakeUp = DateTime.Today + wakeupTime;
if (nextWakeUp < DateTime.Now) nextWakeUp = nextWakeUp.AddDays(1.0);
while (true)
{
TimeSpan ts = nextWakeUp.Subtract(DateTime.Now);
System.Threading.Thread.Sleep((int)ts.TotalMilliseconds);
try { AlarmEvent(this, EventArgs.Empty); }
catch { }
nextWakeUp = nextWakeUp.AddDays(1.0);
}
}
}
I know it's a bit of an old question, but I came across this when I was looking for an answer to something else. I thought I'd throw my two cents in here, since I recently had this particular issue.
Another thing you can do is schedule the method like so:
/// Schedule the given action for the given time.
public async void ScheduleAction ( Action action , DateTime ExecutionTime )
{
try
{
await Task.Delay ( ( int ) ExecutionTime.Subtract ( DateTime.Now ).TotalMilliseconds );
action ( );
}
catch ( Exception )
{
// Something went wrong
}
}
Bearing in mind it can only wait up to the maximum value of int 32 (somewhere around a month), it should work for your purposes. Usage:
void MethodToRun ( )
{
Console.WriteLine ("Hello, World!");
}
void CallingMethod ( )
{
var NextRunTime = DateTime.Now.AddHours(1);
ScheduleAction ( MethodToRun, NextRunTime );
}
And you should have a console message in an hour.
What about System.Timers.Timer class ? See http://msdn.microsoft.com/en-us/library/system.timers.timer.aspx
I have used this before with great success:
Vb.net:
Imports System.Threading
Public Class AlarmClock
Public startTime As Integer = TimeOfDay.Hour
Public interval As Integer = 1
Public Event SoundAlarm()
Public Sub CheckTime()
While TimeOfDay.Hour < startTime + interval
Application.DoEvents()
End While
RaiseEvent SoundAlarm()
End Sub
Public Sub StartClock()
Dim clockthread As Thread = New Thread(AddressOf CheckTime)
clockthread.Start()
End Sub
End Class
C#:
using System.Threading;
public class AlarmClock
{
public int startTime = TimeOfDay.Hour;
public int interval = 1;
public event SoundAlarmEventHandler SoundAlarm;
public delegate void SoundAlarmEventHandler();
public void CheckTime()
{
while (TimeOfDay.Hour < startTime + interval) {
Application.DoEvents();
}
if (SoundAlarm != null) {
SoundAlarm();
}
}
public void StartClock()
{
Thread clockthread = new Thread(CheckTime);
clockthread.Start();
}
}
I don't know if the c# works, but the vb works just fine.
Usage in VB:
Dim clock As New AlarmClock
clock.interval = 1 'Interval is in hours, could easily convert to anything else
clock.StartClock()
Then, just add an event handler for the SoundAlarm event.