xml WPFの的BackgroundWorkerのサンプル
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了xml WPFの的BackgroundWorkerのサンプル相关的知识,希望对你有一定的参考价值。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
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;
using System.ComponentModel;
using System.Windows.Media.Animation;
using System.Threading;
namespace WpfBackGroundWorker
{
/// <summary>
/// Window1.xaml の相互作用ロジック
/// </summary>
public partial class Window1 : Window
{
System.ComponentModel.BackgroundWorker Worker;
public Window1()
{
InitializeComponent();
}
private void btnStart_Click(object sender, RoutedEventArgs e)
{
this.btnStart.IsEnabled = false;
this.btnStop.IsEnabled = true;
this.ProgressBar1.Value = 0;
this.Worker = new System.ComponentModel.BackgroundWorker();
this.Worker.DoWork += new System.ComponentModel.DoWorkEventHandler(Worker_DoWork);
this.Worker.ProgressChanged += new System.ComponentModel.ProgressChangedEventHandler(Worker_ProgressChanged);
this.Worker.RunWorkerCompleted += new System.ComponentModel.RunWorkerCompletedEventHandler(Worker_RunWorkerCompleted);
// 進捗状況の報告をできるようにする
this.Worker.WorkerReportsProgress = true;
// キャンセル処理をできるようにする
this.Worker.WorkerSupportsCancellation = true;
// バックグラウンド処理の実行
this.Worker.RunWorkerAsync();
}
private void btnStop_Click(object sender, RoutedEventArgs e)
{
if (this.Worker == null) return;
// バックグラウンド処理をキャンセルする
this.Worker.CancelAsync();
MessageBox.Show(String.Format("現在値は{0}です", this.ProgressBar1.Value));
}
// 時間のかかる処理を実行
void Worker_DoWork(object sender, System.ComponentModel.DoWorkEventArgs e)
{
for (int value = 1; value <= 100; value++) {
if (this.Worker.CancellationPending)
break;
this.Worker.ReportProgress(value);
// 500mSecスリープ
System.Threading.Thread.Sleep(100);
}
}
// 現在値の更新
void Worker_ProgressChanged(object sender, System.ComponentModel.ProgressChangedEventArgs e)
{
// プログレスバーの現在値を更新する
this.ProgressBar1.Value = e.ProgressPercentage;
}
// 「時間のかかる処理」終了時の処理
void Worker_RunWorkerCompleted(object sender, System.ComponentModel.RunWorkerCompletedEventArgs e)
{
// [START]/[STOP]ボタンの初期化
this.btnStart.IsEnabled = true;
this.btnStop.IsEnabled = false;
this.btnStart.IsEnabled = true;
this.btnStop.IsEnabled = false;
}
}
}
<Window x:Class="WpfBackGroundWorker.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300">
<Grid>
<ProgressBar Name="ProgressBar1" VerticalAlignment="Top" HorizontalAlignment="Left" Margin="12,12,0,0" Height="16" Width="254" Minimum="0" Maximum="100" Value="0"/>
<Button Content="START" Height="23" HorizontalAlignment="Left" Margin="12,34,0,0" Name="btnStart" VerticalAlignment="Top" Width="75" Click="btnStart_Click"/>
<Button Content="STOP" Height="23" HorizontalAlignment="Right" Margin="0,34,110,0" Name="btnStop" VerticalAlignment="Top" Width="75" Click="btnStop_Click"/>
</Grid>
</Window>
以上是关于xml WPFの的BackgroundWorkerのサンプル的主要内容,如果未能解决你的问题,请参考以下文章
WPF 精修篇 BackgroundWorker
Wpf Backgroundworker
BackgroundWorker 是保持 WCF/WPF 应用程序响应的唯一方法吗?
treeview.expandall 方法在 backgroundworker 完成事件 WPF 中不起作用
在WPF 中Dowork事件触发不了
使用BackgroundWorker