文章预览
1. 使用BackgroundWorker组件 代码示例: public partial class MainWindow : Window { private BackgroundWorker backgroundWorker = new BackgroundWorker ( ) ; public MainWindow ( ) { InitializeComponent ( ) ; backgroundWorker . DoWork += BackgroundWorker_DoWork ; backgroundWorker . RunWorkerCompleted += BackgroundWorker_RunWorkerCompleted ; backgroundWorker . WorkerReportsProgress = true ; backgroundWorker . WorkerSupportsCancellation = true ; } private void StartButton_Click ( object sender , RoutedEventArgs e ) { backgroundWorker . RunWorkerAsync ( ) ; } private void BackgroundWorker_DoWork ( object sender , DoWorkEventArgs e ) { // 长时间运行的任务 for ( int i = 0 ; i < 100 ; i ++ ) { if ( backgroundWorker . CancellationPending ) { e . Cancel = true ;
………………………………