//xaml <Window x:Class="WpfApp37.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local="clr-namespace:WpfApp37" WindowState="Maximized" mc:Ignorable="d" Title="MainWindow" Height="450" Width="800"> <Grid TextBlock.FontSize="16"> <Grid.RowDefinitions> <RowDefinition Height="Auto"/> <RowDefinition Height="Auto"/> <RowDefinition Height="Auto"/> <RowDefinition Height="Auto"/> <RowDefinition Height="Auto"/> </Grid.RowDefinitions> <StackPanel Grid.Row="0" Orientation="Horizontal" Margin="6"> <TextBlock Text="From:"/> <TextBox Margin="10,2,2,2" Width="120" MaxLength="10" x:Name="_from"/> <TextBlock Text="To:" Margin="20,0,0,0"/> <TextBox Margin="10,2,2,2" Width="120" MaxLength="10" x:Name="_to"/> </StackPanel> <StackPanel Grid.Row="1" Orientation="Horizontal" Margin="6"> <Button Content="Calculate" x:Name="calcBtn" Padding="4" Click="calc_Click" /> <Button Content="Cancel" Padding="4" Margin="10,0,0,0" IsEnabled="False" x:Name="_cancelBtn" Click="_cancelBtn_Click" /> </StackPanel> <TextBlock Grid.Row="3" x:Name="_result" FontSize="20" Margin="6" HorizontalAlignment="Center"/> </Grid> </Window> //cs using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.ComponentModel; using System.Linq; using System.Linq.Expressions; using System.Text; using System.Threading; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; namespace WpfApp37 { /// <summary> /// Interaction logic for MainWindow.xaml /// </summary> public partial class MainWindow : Window { CancellationTokenSource _cts; public MainWindow() { InitializeComponent(); } static int CountPrimes(int from, int to,CancellationToken ct) { int total = 0; for (int i = from; i <= to; i++) { if (ct.IsCancellationRequested) { return total; } bool isPrime = true; int limit = (int)Math.Sqrt(i); for (int j = 2; j <= limit; j++) { if (i % j == 0) { isPrime = false; break; } } if (isPrime) { total++; } } return total; } private void calc_Click(object sender, RoutedEventArgs e) { _result.Text = "Calculating..."; int first=int.Parse(_from.Text),last=int.Parse(_to.Text); calcBtn.IsEnabled = false; _cancelBtn.IsEnabled = true; _cts=new CancellationTokenSource(); ThreadPool.QueueUserWorkItem(x => { int total = CountPrimes(first, last, _cts.Token); Dispatcher.BeginInvoke(new Action(() => { _result.Text = _cts.IsCancellationRequested ? $"Cancelled to :{total}" : $"Finished Total Primes :{total}"; _cancelBtn.IsEnabled = false; calcBtn.IsEnabled = true; })); }); } private void _cancelBtn_Click(object sender, RoutedEventArgs e) { if (_cts != null) { _cts.Cancel(); } } } }
标签:IsCancellationRequested,cts,Windows,System,CancellationTokenSource,int,Cancel,us From: https://www.cnblogs.com/Fred1987/p/18117974