Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c# autoclose window with sleep but text disappears

Tags:

c#

sleep

timer

The text "lblDate.Content" disappears when I use the sleep timer to close the window. How do I get that text to display? The rest of the window/text is shown. I'm open to other ways to autoclose a window.

public void DisplayErrorMessage(string message)
{
    //  Error Message TextBox
        textBox1.Text = message;
        Show();

    // Show date and logged message
        lblDate.Content = "This error has been logged and an administrator contacted:  " + DateTime.Now;

    // Auto close window
        System.Threading.Thread.Sleep(3000);
        this.Close();
}
like image 629
rd42 Avatar asked Mar 16 '11 15:03

rd42


2 Answers

Sleep is not a timer. It is a command to tell the thread to stop processing (blocks the thread) for an amount of time. In this case you are blocking the application thread. If your intention is to keep the window open for 3 seconds before closing it you should may want to check out the Timer class.

like image 68
Andrew Avatar answered Sep 23 '22 13:09

Andrew


Perhaps Application.DoEvents() to update the drawing of your controls could help?

like image 29
Joachim VR Avatar answered Sep 21 '22 13:09

Joachim VR