好的,所以我有一个存储记录并执行各种其他功能的应用程序,我希望能够实现一种创建某种警报系统的方法,当截止日期(与任何记录一起存储)临近时它会告诉我/用户。不确定这是否可能,但任何建议或建议都会非常感谢。
问问题
1574 次
2 回答
1
使用 System.DateTime 您应该能够像这样访问机器的当前日期
DateTime today = DateTime.Today;
您可以定期将其与记录中存储的时间(可能是应用程序的开始时间?)进行比较,以检查截止日期是否临近。
于 2014-03-06T10:25:39.237 回答
1
您可以启动一个单独的任务,以永远阻塞的方式遍历一堆记录,并检查您是否需要采取一些措施。这是一个有效的 WPF 示例,您可以打开这个概念。我认为您可以弄清楚如何将其应用于您的特定需求。
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; }
}
}
此示例的输出将类似于:
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
---------------
快乐编码!
于 2014-03-06T16:34:36.767 回答