
Question:
As a training exercise, I am creating a counter that runs in a background thread, in a .NET 3.5 WPF app. The idea is that the counter gets incremented and every few seconds the total is output to the screen.
As it is a training exercise, the foreground thread doesn't really do anything, but imagine that it does!
There is a start and stop button to start and stop the counter.
The problem is that I want the foreground thread to poll the total every few seconds and update it in the text box on the form. The entire app goes 'Not Responding' when I try this. Currently I have only got it to update the count a fixed time after it starts it.
I'll dump all the code in here, so that you get the whole picture:
public partial class Window1 : Window { public delegate void FinishIncrementing(int currentCount); private int reportedCount; private Thread workThread; public Window1() { InitializeComponent(); } #region events private void Window_Loaded(object sender, RoutedEventArgs e) { txtCurrentCount.Text = "0"; reportedCount = 0; InitialiseThread(); } private void btnStart_Click(object sender, RoutedEventArgs e) { workThread.Start(); // Do some foreground thread stuff Thread.Sleep(200); // I want to put this in a loop to report periodically txtCurrentCount.Text = reportedCount.ToString(); } private void btnStop_Click(object sender, RoutedEventArgs e) { workThread.Abort(); txtCurrentCount.Text = reportedCount.ToString(); InitialiseThread(); }
Next
« Prev Post
« Prev Post
Previous
Next Post »
Next Post »
EmoticonEmoticon