WPF模式对话框上的计时器

时间:2016-10-12 20:15:25

标签: c# wpf multithreading

我正在尝试从WPF MainWindow启动模式对话框。模态对话框包含两个按钮"立即安装"和"稍后安装"。一旦MainWindow启动模态对话框,计时器将从30秒开始。它等待用户单击两个按钮中的任何一个。如果用户在30秒内未点击任何按钮,则关闭模态窗口并继续进行MainWindow。 MainWindow到现在等待模态窗口关闭。

以下是主窗口的代码:

InstallNotify objInstallNotify = null;
                        Dispatcher.Invoke(new Action(() =>
                        {
                            Logger.WriteToLog("Displaying Notification Dialog");
                            objInstallNotify = new InstallNotify();
                            objInstallNotify.ShowDialog();

                        }));

if (objInstallNotify.ISPOSTPONECLICKED)
                            return;

InstallNotify中的代码如下:

public partial class InstallNotify : Window
{
    private static int totaltime = 30;
    //System.Threading.Timer dispatcherTimer = new System.Threading.Timer(dispatcherTimer_Tick1,null,1000,1000);
    private System.Timers.Timer objTimer = new System.Timers.Timer();
    private bool bPostponeBtnClicked = false;
    private bool bInstallNowBtnClicked = false;

    public bool ISPOSTPONECLICKED
    {
        get
        {
            return bPostponeBtnClicked;
        }
    }

    public bool ISINSTALLNOWCLICKED
    {
        get
        {
            return bInstallNowBtnClicked;
        }
    }

    /// <summary>
    /// Constructor
    /// </summary>
    public InstallNotify()
    {
        InitializeComponent();
    }

    private void InstallNotifyLoaded(object sender, RoutedEventArgs e)
    {
        Logger.WriteToLog("Launched dialog");
        this.objTimer.Interval = 1000; //1 sec
        this.objTimer.Elapsed += new System.Timers.ElapsedEventHandler(TTTimer_Elapsed);
        this.objTimer.Start();
    }

    private void TTTimer_Elapsed(object sender, ElapsedEventArgs e)
    {
        this.Dispatcher.Invoke((Action)delegate()
        {
            TimeLeftInSecs.Content = totaltime.ToString();
            Logger.WriteToLog("Timer Count = " + totaltime);
        });
        totaltime--;
        if (totaltime < 0)
        {
            objTimer.Stop();
            if (this.Dispatcher.CheckAccess())
            {
                Logger.WriteToLog("Closing form Access - True");
                this.Close();
            }
            else
            {
                this.Dispatcher.Invoke((System.Action)delegate
                {
                    Logger.WriteToLog("Closing form Access - False");
                    this.Close();
                });
            }
        }

    }

    private void dispatcherTimer_Tick(object sender, EventArgs e)
    {
        TimeLeftInSecs.Content = totaltime.ToString();
        totaltime--;
        if (totaltime < 0)
        {
            //dispatcherTimer.Stop();
            this.Close();
        }
    }

    /// <summary>
    /// This event handler is called when user clicks on the postpone button
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void PostponeBtnClicked(object sender, RoutedEventArgs e)
    {
        bPostponeBtnClicked = true;
        this.Close();
    }


    /// <summary>
    /// This event handler is called when user clicks on the Install Now button
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void InstallNowClicked(object sender, RoutedEventArgs e)
    {
        bInstallNowBtnClicked = true;
        this.Close();
    }

}

当我尝试运行此代码时,它工作正常。 现在我通过Windows任务调度程序调用我的可执行文件并锁定我的屏幕。这次它不会写消息&#34;显示通知对话框&#34;在日志文件而不是程序失败,但有例外:

  

例外情况是:操作成功完成   2016年10月13日星期四上午1:33:24:异常的Stack Trace   在MS.Win32.SafeNativeMethods.ReleaseCapture()处   System.Windows.Window.ShowDialog()at   MainWindow。&lt;&gt; c__DisplayClass27.b__24()at   System.Windows.Threading.DispatcherOperation.InvokeDelegateCore()
  在System.Windows.Threading.DispatcherOperation.InvokeImpl()

这里有什么问题?

0 个答案:

没有答案