-
February 18th, 2012, 08:02 AM
#1
pause or sleep function in wpf not working
Hi!
I have developed a very small application using wpf. I used a canvas with name/id myCanvas. Then i placed a button and textbox controls on this canvas. I wanted that when i clik on button then my textbox display numbers from 1 to 10 with one second duration. Means, textbox diplay 1 then wait for 1 second then display 2 and again wait for 1sec then 3 and again wait for 1second and so on till 10 (..it is a counter. My code for this is as:
namespace numberGenDelayThread
{
public partial class MainWindow : Window
{
int n = 1;
public MainWindow()
{
InitializeComponent();
}
private void btn_Start_Click(object sender, RoutedEventArgs e)
{
while (n < =10)
{
txt_Number.Text = n.ToString();
delay(1000);
n++;
}
}
void delay(double ms)
{
DateTime dt = DateTime.Now.AddMilliseconds(ms);
while (DateTime.Now <= dt)
{
;
}
}
}
}
Problem: This program waits for almost 9 seconds and then display only 10 in textbox (with name txt_Number in code).
It did not display numbers from 1 to 10 with 1second duration...in other words it did not wrok like counter. Why this so?
how can i solve this?
should i use canvas or some other container?
how can i achieve this in wpf?
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|