Question

I am having some problems with using some of Dispatcher Timers on a WPF window. On a window I would usually use a timer, but this function doesn't seem to be present on WPF forms so I was advised that DispatcherTimer was the equivalent.

So I have 3 of these timers:

The first one every 30 seconds brings the form forward - this one works correctly.

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);
}

The second one keeps checking every 100 milliseconds to see if IExplorer is running and if so hides the OK button and shows a message on the forms telling the user to close IExplorer - When you run the form if IE is running is will disable the button and show the message, but after you close IE it doesn't change it back.

What could i do to get the timer to constantly run and update the form if IE is opened or closed?

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;
    }
}

And Thirdly, like the second timer runs every 100 milliseconds, once they have click on the OK button, I want to to kill the IExplorer process in the event that the user tries to invoke it, but again like the second timer is doesn't seem to be running constantly.

Any ideas?

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();
    }
}
Was it helpful?

Solution

if IE is running is will disable the button and show the message, but after you close IE it doesn't change it back. This is happening because you are not getting process in timer tick event. So change you code as shown below.

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;
    }
}

OTHER TIPS

In the code snippet, you only get the list of the processes once and then check the same array every time. If it is the same in your real code, make sure you update the process list with each tick.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top