截止日期临近时的C#警报

时间:2014-03-06 10:20:20

标签: c# date ms-access-2007 alert deadlines

好的,所以我有一个应用程序存储记录并执行各种其他功能,我希望能够实现一种创建某种警报系统的方法,其中截止日期(存储在任何记录中)接近它会告诉我/用户。不确定这是否可行,但任何建议或建议都会非常感谢。

2 个答案:

答案 0 :(得分:1)

使用System.DateTime,你应该可以像这样访问机器的当前日期

DateTime today = DateTime.Today;

你可以将它与常规intervall(应用程序的开始可能?)记录中存储的时间进行比较,以检查截止日期是否接近。

答案 1 :(得分:1)

你可以启动一个单独的任务来永久地以nob-blocking方式迭代一堆记录,并检查你是否需要采取一些行动。这是一个有效的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
---------------

快乐的编码!