Click here to Skip to main content
15,892,072 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello

I am new in C#.

I want to make an basic app that shows two messages in a different text boxes by clicking one button. The second message most be appeared after two seconds.

I have tried the following code that dose not work. Indeed both of the messages are shown at once after two seconds.

What I have tried:

private void btn_play_Click(object sender, RoutedEventArgs e)
        {
                tb_hello.Text = "hello";
                Thread.Sleep(2000);
                tb_goodbye.Text = "goodbye";
        }
Posted
Updated 19-Jun-18 10:40am

I would use the Task-based Asynchronous Pattern. await Task.Delay(2000) is an asynchronous 'wait' it does not block the UI thread. Note the async keyword in the method's signature.


C#
private async void btn_play_Click(object sender, RoutedEventArgs e)
       {
           tb_hello.Text = "hello";
           await Task.Delay(2000);
           tb_goodbye.Text = "goodbye";
       }
 
Share this answer
 
v2
Thread.Sleep suspends the current thread - which means that nothing at all happens until the period has elapsed. And that includes updates to display controls! Since you are updating UI controls, that code is running on the UI thread (or you'd get a cross threading exception) which means that Paint requests to update the actual display can't be honoured until the thread has finished sleeping, and the event handler exits.

Instead, show the first message then start a timer to replace it with the second.
In practice, I'd have a timer running full time, and use a counter that is reduced in the timer event handler to decide when to replace the message - it's a simpler solution that doesn't risk multiple timers running at the same time - they use resources that are scarce so that's a bad idea.
 
Share this answer
 
Comments
bamshad1986 19-Jun-18 11:58am    
I need both the messages be shown in the last screen. I tested while loop with time clock but it did not work.
OriginalGriff 19-Jun-18 12:04pm    
So don't replace it - use a second control as you show in your example. Just move that bit into the timer event handler and execute it after the timeout period.
OriginalGriff 19-Jun-18 12:07pm    
You've got to remember how Windows works: Paint events are very low priority (in fact, WM_PAINT messages are low priority, but they generate the Paint events) and are never actioned while you code is executing other event handlers.

You have to use a timer to let your code complete, and then set your new text in the timer code.
Use one or more DispatcherTimers to update independent controls; particularly in WPF.

Since they run "on the UI thread", they are ideally suited for updating controls.

The DispatcherTimer - The complete WPF tutorial[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900