/// <summary>
/// Interaction logic for ThreadTest.xaml
/// </summary>
public
partial
class
ThreadTest : Window
{
public
delegate
void
NextPrimeDelegate();
private
long
num = 3;
private
bool
continueCalculating =
false
;
public
ThreadTest()
{
InitializeComponent();
}
private
void
StartOrStop(
object
sender, RoutedEventArgs e)
{
if
(continueCalculating)
{
continueCalculating =
false
;
startStopButton.Content =
"Resume"
;
}
else
{
continueCalculating =
true
;
startStopButton.Content =
"Stop"
;
startStopButton.Dispatcher.BeginInvoke(
DispatcherPriority.Normal,
new
NextPrimeDelegate(CheckNextNumber));
}
}
public
void
CheckNextNumber()
{
NotAPrime =
false
;
for
(
long
i = 3; i <= Math.Sqrt(num); i++)
{
if
(num % i == 0)
{
NotAPrime =
true
;
break
;
}
}
if
(!NotAPrime)
{
bigPrime.Text = num.ToString();
}
num += 2;
if
(continueCalculating)
{
startStopButton.Dispatcher.BeginInvoke(
System.Windows.Threading.DispatcherPriority.SystemIdle,
new
NextPrimeDelegate(
this
.CheckNextNumber));
}
}
private
bool
NotAPrime =
false
;
private
void
startStopButton2_Click(
object
sender, RoutedEventArgs e)
{
MessageBox.Show(
"Hello Thread"
);
}
private
void
startStopButton3_Click(
object
sender, RoutedEventArgs e)
{
long
n = 0;
while
(n < 10000000)
{
myData.Text = n.ToString();
n++;
}
}
}