Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <Window x:Class="ThreadingPrimeNumberSample.MainWindow"
- xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
- xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
- Title="Prime Numbers" Width="760" Height="500">
- <Grid>
- <Button Content="Start"
- Click="StartOrStop"
- x:Name="startStopButton"
- Margin="10,10,693,433"
- />
- <TextBlock Margin="87,15,547,424"><Run Text="Biggest Prime Found:"/><InlineUIContainer>
- </InlineUIContainer></TextBlock>
- <TextBlock x:Name="bigPrime" Margin="222,15,409,428"><Run Text="3"/></TextBlock>
- <TextBox Height="104" TextWrapping="Wrap" Text="TextBox" Width="522" Margin="87,295,143,70"/>
- </Grid>
- </Window>
- ...usings...
- namespace ThreadingPrimeNumberSample
- {
- public partial class MainWindow : Window
- {
- public delegate void NextPrimeDelegate();
- //Current number to check
- private long num = 3;
- private bool continueCalculating = false;
- public MainWindow()
- : base()
- {
- InitializeComponent();
- }
- private void StartOrStop(object sender, EventArgs 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()
- {
- // Reset flag.
- NotAPrime = false;
- for (long i = 3; i <= Math.Sqrt(num); i++)
- {
- if (num % i == 0)
- {
- // Set not a prime flag to true.
- NotAPrime = true;
- break;
- }
- }
- // If a prime number.
- if (!NotAPrime)
- {
- bigPrime.Text = num.ToString();
- }
- num += 2;
- Thread.Sleep(500);
- if (continueCalculating)
- {
- startStopButton.Dispatcher.BeginInvoke(
- System.Windows.Threading.DispatcherPriority.SystemIdle,
- new NextPrimeDelegate(this.CheckNextNumber));
- }
- }
- private bool NotAPrime = false;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement