1

From the MainWindow I launch another window with two buttons and a label. The purpose is to use the label as a time counter from 30 to 0. Basically the application waits for user to press any of the buttons within 30 seconds. If any of them is clicked or the timeout occurs, the window is closed. Here us the code:

public partial class InstallNotify : Window
{
    private int totaltime = 30;
    DispatcherTimer dispatcherTimer = new DispatcherTimer();
    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)
    {
        dispatcherTimer.Tick += new EventHandler(dispatcherTimer_Tick);
        dispatcherTimer.Interval = new TimeSpan(0,0,1);
        dispatcherTimer.Start();
    }

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

}

What happens now is that the dispatcherTimer_Tick updates the label for a few seconds and then stops. The application freezes then. I am not able to click on button as well as the counter does not change.

The King
  • 833
  • 1
  • 15
  • 41
  • Can't see anything strange in the code you provided. Perhaps the code of the other window? What happens if you press the `Break all` when your application hangs? When does the gui thread hangs? – Jeroen van Langen Jan 31 '17 at 10:23

0 Answers0