Created
April 7, 2014 02:11
-
-
Save yoshikazuendo/10013910 to your computer and use it in GitHub Desktop.
WPFのBackgroundWorkerのサンプル
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment