using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Threading;
namespace Threading
{
public partial class Page : UserControl
{
private Thread _thread1;
private Thread _thread2;
private Thread _thread3;
private static System.Windows.Controls.TextBlock _data1;
private static System.Windows.Controls.TextBlock _data2;
private static System.Windows.Controls.TextBlock _data3;
private static int _count1 = 0;
private static int _count2 = 0;
private static int _count3 = 0;
private static bool _done = true;
public bool Done
{
get { return _done; }
set { _done = value; }
}
public Page()
{
InitializeComponent();
_data1 = Data1;
_data2 = Data2;
_data3 = Data3;
}
private void StartThreads()
{
_done = false;
_thread1 = new Thread(DoThread1);
_thread1.Start();
_thread2 = new Thread(DoThread2);
_thread2.Start();
_thread3 = new Thread(DoThread3);
_thread3.Start();
}
public static void DoThread1()
{
while (!_done)
{
_count1++;
_data1.Dispatcher.BeginInvoke(delegate()
{
_data1.Text = _count1.ToString();
});
System.Threading.Thread.Sleep(100);
}
}
public static void DoThread2()
{
while (!_done)
{
_count2++;
_data2.Dispatcher.BeginInvoke(delegate()
{
_data2.Text = _count2.ToString();
});
System.Threading.Thread.Sleep(100);
}
}
public static void DoThread3()
{
while (!_done)
{
_count3++;
_data3.Dispatcher.BeginInvoke(delegate()
{
_data3.Text = _count3.ToString();
});
System.Threading.Thread.Sleep(100);
}
}
private void Button_Click(object sender, RoutedEventArgs e)
{
if (false == _done)
_done = true;
else
StartThreads();
}
}
}