重置我的DispatcherTimer

时间:2017-03-02 09:34:30

标签: wpf

我正在使用WPF应用程序,我每天从早上7点开始提醒每天凌晨3点,即使用户关闭了窗口,它也会每小时运行一次,它会在一小时后使用DispatcherTimer显示。

我的问题是在下午3点之后关闭并且它继续检查告诉第二天在7点之后计算一小时以显示窗口但不是。

它在上午7点后直接显示。我如何解决它,请协助

我的代码:

public partial class MainWindow : Window
    {
        public int _mCollapsed;
        public Window1 WindowLink1;
        public ReminderWin ReminderWin1;

        public TimeSpan start = new TimeSpan(7, 0, 0);
        public TimeSpan end = new TimeSpan(15, 0, 0);

        public MainWindow()
        {
            InitializeComponent();
            _mCollapsed = 1;
            WindowLink1 = new Window1();
            ReminderWin1 = new ReminderWin();
        }
       private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            TimeSpan now = DateTime.Now.TimeOfDay;
            if (now > start)
            {
                System.Windows.Threading.DispatcherTimer dispatcherTimer = new System.Windows.Threading.DispatcherTimer();
                dispatcherTimer.Interval = new TimeSpan(1, 0, 0);
                dispatcherTimer.Tick += new EventHandler(dispatcherTimer_Tick);
                dispatcherTimer.Start();
            }
private void dispatcherTimer_Tick(object sender, EventArgs e)
        {
            TimeSpan now = DateTime.Now.TimeOfDay;
            if ((now > start) && (now < end))
            {                               
                BitmapImage _image = new BitmapImage();
                _image.BeginInit();
                _image.UriSource = new Uri(@"test123.jpg", UriKind.RelativeOrAbsolute);
                _image.UriCachePolicy = new RequestCachePolicy(RequestCacheLevel.BypassCache);
                _image.CacheOption = BitmapCacheOption.OnLoad;
                _image.CreateOptions = BitmapCreateOptions.IgnoreImageCache;
                _image.EndInit();
                ReminderWin1.image1.Source = _image;

                System.Diagnostics.Debug.WriteLine(ReminderWin1.WindowState);

                if (ReminderWin1.Visibility == Visibility.Collapsed || ReminderWin1.Visibility == Visibility.Hidden)
                {
                    ReminderWin1.Visibility = System.Windows.Visibility.Visible;
                }
            }
            else if (now > end)
            {
                if (ReminderWin1.Visibility == Visibility.Visible || ReminderWin1.Visibility == Visibility.Hidden)
                {
                    ReminderWin1.Close();
                }
            }
        }

        }

0 个答案:

没有答案