MainWindow.xaml
<Window x:Class="WpfApplication5.MainWindow"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Height="200" Width="200">
<StackPanel>
<StackPanel Orientation="Horizontal">
<TextBlock Text="0.5秒ごとにカウント:" />
<TextBlock Name="fastTextBlock" Text="{Binding Path=Progress}" />
</StackPanel>
<StackPanel Orientation="Horizontal">
<TextBlock Text="1.0秒ごとにカウント:" />
<TextBlock Name="slowTextBlock" Text="{Binding Path=Progress}" />
</StackPanel>
</StackPanel>
</Window>
MainWindow.xaml.cs
using System.Threading;using System.Threading.Tasks;
using System.Windows;
namespace WpfApplication5
{
public partial class MainWindow : Window
{
private Message message1 = new Message();
private Message message2 = new Message();
public MainWindow()
{
InitializeComponent();
this.fastTextBlock.DataContext = this.message1;
this.slowTextBlock.DataContext = this.message2;
Task.Factory.StartNew(obj => LongRunningMethod1((Message)obj), this.message1);
Task.Factory.StartNew(obj => LongRunningMethod2((Message)obj), this.message2);
}
private void LongRunningMethod1(Message message)
{
for (int i = 0; i < 100; i++)
{
message.Progress = i.ToString();
Thread.Sleep(500);
}
}
private void LongRunningMethod2(Message message)
{
for (int i = 0; i < 100; i++)
{
message.Progress = i.ToString();
Thread.Sleep(1000);
}
}
}
}
Message.cs
using System;using System.ComponentModel;
namespace WpfApplication5
{
public class Message : INotifyPropertyChanged
{
private string progress;
public string Progress
{
get { return progress; }
set
{
if (value != this.progress)
{
this.progress = value;
NotifyPropertyChanged("Progress");
}
}
}
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged(String info)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(info));
}
}
}
}
0 件のコメント:
コメントを投稿