More Related Content
PDF
Unityネットワーク通信の基盤である「RPC」について、意外と知られていないボトルネックと、その対策法
モノビット エンジン
What's hot (20) PDF
PHPでWebSocketを実装してみてわかったこと
ksimoji
PDF
転置インデックスとTop k-query
正志 坪坂
PPTX
C#や.NET Frameworkがやっていること
信之 岩永
PDF
ドメイン駆動設計サンプルコードの徹底解説
増田 亨
Similar to Wpfと非同期 (20) PPTX
An other world awaits you
信之 岩永
PPTX
async/await のしくみ
信之 岩永
PPTX
Orange Cube 自社フレームワーク 2015/3
信之 岩永
More from yone64 (7)
PPTX
もう一つのLINQ ~ Queryable入門
yone64
Recently uploaded (11)
PPTX
2025_7_25_吉祥寺_設計ナイト_ADR運用におけるデータ利活用の考え方.pptx
ssuserfcafd1
Wpfと非同期
5. TaskSchedulerで、同期コンテキストを指定する。
var t = Task.Factory.StartNew(() =>
{
var s = textBox1.Text;
},
CancellationToken.None,
TaskCreationOptions.None,
TaskScheduler.FromCurrentSynchronizationContext());
6. Dispatcherを使い実行する。
◦ Invoke, BeginInvoke, InvokeAsyncなど
var t = Task.Run(() =>
{
textBox1.Dispatcher.Invoke(() =>
{
var s = textBox1.Text;
});
});
7. 優先度付キュー
◦ 優先度が高いものから実行されていく
DispatcherPriority
◦ Send
◦ Normal
◦ DataBind
◦ Render
◦ Loaded
◦ Input
◦ Background
◦ ContextIdle
◦ ApplicationIdle
◦ SystemIdle
◦ Inactive
◦ Invalid
Send
Normal
DataBind
Render
① ②
③
8. var dispatcher = Application.Current.Dispatcher;
dispatcher.BeginInvoke(new Action(() => Console.WriteLine("No1")),
DispatcherPriority.Normal);
Console.WriteLine("No2");
dispatcher.BeginInvoke(new Action(() => Console.WriteLine("No3")),
DispatcherPriority.Render);
dispatcher.BeginInvoke(new Action(() => Console.WriteLine("No4")),
DispatcherPriority.Send);
dispatcher.Invoke(new Action(() => Console.WriteLine("No5")),
DispatcherPriority.DataBind);
Console.WriteLine("No6");
14. BitmapImageやBrushをバックグラウンドで作成し、
利用する。
Task.Run(() =>
{
var sr = new MemoryStream(
new WebClient().DownloadData(new Uri("http://xx/img/1.jpg")));
var bi = new BitmapImage();
bi.BeginInit();
bi.CacheOption = BitmapCacheOption.OnLoad;
bi.StreamSource = sr;
bi.EndInit();
bi.Freeze();
Application.Current.Dispatcher.Invoke(() =>
{
image.Source = bi;
});
});
15. メッセージプールを複数作成する。
var thread = new Thread(() =>
{
var dispacherFrame = new DispatcherFrame(true);
var w = new Window {Width = 800, Height = 600};
var p = new WrapPanel {Orientation = Orientation.Horizontal};
w.Content = p;
for (int i = 0; i < 20000; i++)
{
p.Children.Add(new Button
{
Height = 30,
Width = 120,
Content = "ボタン" + i,
});
}
w.Closed += (o, args) => dispacherFrame.Continue = false;
w.Show();
Dispatcher.PushFrame(dispacherFrame);
});
thread.SetApartmentState(ApartmentState.STA);
thread.Start();
20. メッセージループをアプリケーション内で複数作ってみ
る - 亀岡的プログラマ日記
◦ http://posaune.hatenablog.com/entry/2013/05/21
/010735
ReactiveProperty
◦ http://reactiveproperty.codeplex.com/