Reputation: 139
Ok so I have an application that stores records and does various other functions, I would like to be able to implement a way of creating some sort of alert system where by when a deadline date (stored with any record) is approaching it will tell me/user. Not sure if this is possible but any advice or suggestions would be great thanks.
Upvotes: 1
Views: 1941
Reputation: 8404
You could fire up a separate task to iterate through bunch of records in non-blocking manner forever and check if you need to take some action. Here's a working WPF example for you to open up the concept. I think you can figure out how to apply it to your specific needs.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
namespace WpfApplication
{
public partial class MainWindow : Window
{
private readonly List<MyEvent> _myEvents;
public MainWindow()
{
InitializeComponent();
DataContext = this;
// Add some dummy events occuring on specific time
_myEvents = new List<MyEvent>()
{
new MyEvent("My event A", DateTime.Now.AddSeconds(5)),
new MyEvent("My event B", DateTime.Now.AddSeconds(10)),
new MyEvent("My event C", DateTime.Now.AddSeconds(20))
};
// Fire up the event iterator
Task.Factory.StartNew(() =>
{
while (true)
{
// Report events' status
DateTime now = DateTime.Now;
foreach (var myEvent in _myEvents.Where(e => e.Time <= now))
System.Diagnostics.Debug.WriteLine(string.Format("Event '{0}' already held", myEvent.Name));
foreach (var myEvent in _myEvents.Where(e => e.Time > now))
{
string notification = "Event '{0}' at '{1}' starting in {2} seconds";
TimeSpan timeSpan = myEvent.Time - now;
notification = string.Format(notification, myEvent.Name, myEvent.Time, (int)timeSpan.TotalSeconds);
System.Diagnostics.Debug.WriteLine(notification);
}
System.Diagnostics.Debug.WriteLine(new string('-', 15));
// Wait for a while before next iteration
Thread.Sleep(3000);
}
});
}
}
// Dummy
public class MyEvent
{
public MyEvent()
{}
public MyEvent(string name, DateTime time)
{
Name = name;
Time = time;
}
public string Name { get; set; }
public DateTime Time { get; set; }
}
}
Output from this example would be something like:
Event 'My event A' at '6.3.2014 18:34:02' starting in 4 seconds
Event 'My event B' at '6.3.2014 18:34:07' starting in 9 seconds
Event 'My event C' at '6.3.2014 18:34:17' starting in 19 seconds
---------------
Event 'My event A' at '6.3.2014 18:34:02' starting in 1 seconds
Event 'My event B' at '6.3.2014 18:34:07' starting in 6 seconds
Event 'My event C' at '6.3.2014 18:34:17' starting in 16 seconds
---------------
Event 'My event A' already held
Event 'My event B' at '6.3.2014 18:34:07' starting in 3 seconds
Event 'My event C' at '6.3.2014 18:34:17' starting in 13 seconds
---------------
Event 'My event A' already held
Event 'My event B' at '6.3.2014 18:34:07' starting in 0 seconds
Event 'My event C' at '6.3.2014 18:34:17' starting in 10 seconds
---------------
Happy coding!
Upvotes: 2
Reputation: 788
Using System.DateTime you should be able to access the current date of the machine like this
DateTime today = DateTime.Today;
You could compare this with the stored time in your records at a regular intervall (start of the application maybe?) to check if the deadline is approaching.
Upvotes: 1