0

我在 WPF 窗口上使用某些调度程序计时器时遇到了一些问题。在窗口上,我通常会使用计时器,但 WPF 表单上似乎不存在此功能,因此我被告知这DispatcherTimer是等效的。

所以我有 3 个这样的计时器:

第一个每 30 秒将表单向前推进 - 这个工作正常。

dispatcherTimer1.Tick += new EventHandler(dispatcherTimer1_Tick);
dispatcherTimer1.Interval = TimeSpan.FromSeconds(30);
dispatcherTimer1.Start();

private void dispatcherTimer1_Tick(object sender, EventArgs e)
{
    this.Topmost.Equals(true);
    this.Activate();
    this.BringIntoView();
    this.Focus();
    this.Topmost.Equals(false);
}

第二个每隔 100 毫秒检查一次 IExplorer 是否正在运行,如果是,则隐藏 OK 按钮并在表单上显示一条消息,告诉用户关闭 IExplorer - 当您运行表单时,如果 IE 正在运行,则会禁用该按钮和显示消息,但在您关闭 IE 后它不会将其更改回来。

如果 IE 打开或关闭,我该怎么做才能让计时器不断运行并更新表单?

public Process[] aProc = Process.GetProcessesByName("IExplore");

dispatcherTimer2.Tick += new EventHandler(dispatcherTimer2_Tick);
dispatcherTimer2.Interval = TimeSpan.FromMilliseconds(100);
dispatcherTimer2.Start();

private void dispatcherTimer2_Tick(object sender, EventArgs e)
{
    if (aProc.Length == 0)
    {
        richTextBox3.Visibility = System.Windows.Visibility.Hidden;
        button1.Visibility = System.Windows.Visibility.Visible;
    }
    else
    {
        button1.Visibility = System.Windows.Visibility.Hidden;
        richTextBox3.Visibility = System.Windows.Visibility.Visible;
    }
}

第三,就像第二个计时器每 100 毫秒运行一次一样,一旦他们点击了 OK 按钮,我想在用户尝试调用它的情况下终止 IExplorer 进程,但再次像第二个计时器一样似乎一直在运行。

有任何想法吗?

dispatcherTimer3.Tick += new EventHandler(dispatcherTimer3_Tick);
dispatcherTimer3.Interval = TimeSpan.FromMilliseconds(100);
dispatcherTimer3.Start();

private void dispatcherTimer3_Tick(object sender, EventArgs e)
{
    Process[] Processes = Process.GetProcessesByName("IExplore");

    foreach (Process Proc1 in Processes)
    {
        Proc1.Kill();
    }
}
4

2 回答 2

4

如果 IE 正在运行,则会禁用该按钮并显示消息,但在您关闭 IE 后它不会将其更改回来。发生这种情况是因为您没有在计时器滴答事件中获得进程。因此,如下所示更改您的代码。

dispatcherTimer2.Tick += new EventHandler(dispatcherTimer2_Tick);
dispatcherTimer2.Interval = TimeSpan.FromMilliseconds(100);
dispatcherTimer2.Start();

private void dispatcherTimer2_Tick(object sender, EventArgs e)
{
    Process[] aProc = Process.GetProcessesByName("IExplore"); 
    if (aProc.Length == 0)
    {
       richTextBox3.Visibility = System.Windows.Visibility.Hidden;
       button1.Visibility = System.Windows.Visibility.Visible;
    }
    else
    {
       button1.Visibility = System.Windows.Visibility.Hidden;
       richTextBox3.Visibility = System.Windows.Visibility.Visible;
    }
}
于 2014-03-26T11:48:51.360 回答
0

在代码片段中,您只获取一次进程列表,然后每次检查相同的数组。如果在您的真实代码中是相同的,请确保在每次勾选时更新进程列表。

于 2014-03-26T11:19:11.223 回答