私は現在オフィスアドインに取り組んでおり、進行状況を表示する通知ダイアログを表示する必要があります.Philipp Sumiの wpf-notifyiconを使用しています.
メインスレッドで既に実行されているコードがたくさんあるため、別のスレッドから通知アイコンを表示する必要があります。これにより、Windows メッセージキュー内のメッセージが処理されていないため、wpf-notifyicon がブロックされて待機します。
この時間のかかるコードを別のスレッドで実行し、メイン スレッドから通知アイコンを表示し、それに応じて更新する必要があることはわかっていますが、残念ながら、このソリューション全体がシングル スレッドであるため、それは代替手段ではありません。
例:
private FancyPopup fancyPopup;
private void button1_Click(object sender, EventArgs e)
{
notifyIcon = new TaskbarIcon();
notifyIcon.Icon = Resources.Led;
fancyPopup = new FancyPopup();
Thread showThread = new Thread(delegate()
{
notifyIcon.ShowCustomBalloon(fancyPopup, System.Windows.Controls.Primitives.PopupAnimation.Fade, null);
});
showThread.Start();
}
private void button2_Click(object sender, EventArgs e)
{
fancyPopup.TextB.Text = "Doing something...";
//Keep the main thread busy.
Thread.Sleep(5000);
fancyPopup.TextB.Text = "Done doing something...";
}
更新 私はこの更新されたコードでもう少し進歩することができました:
新しいスレッドで TaskbarIcon オブジェクトを作成し、Application.Run を使用してそのスレッドでアプリケーション メッセージ ループを処理しています...
private FancyPopup fancyPopup;
private void button1_Click(object sender, EventArgs e)
{
Thread showThread = new Thread(delegate()
{
notifyIcon = new TaskbarIcon();
notifyIcon.Icon = Resources.Led;
fancyPopup = new FancyPopup();
notifyIcon.ShowCustomBalloon(fancyPopup, System.Windows.Controls.Primitives.PopupAnimation.Fade, null);
System.Windows.Forms.Application.Run();
});
showThread.SetApartmentState(ApartmentState.STA);
showThread.Start();
}
private void button2_Click(object sender, EventArgs e)
{
fancyPopup.Dispatcher.Invoke(new Action(delegate
{
fancyPopup.TextB.Text = "Doing something...";
}));
//Keep the main thread busy.
Thread.Sleep(5000);
fancyPopup.Dispatcher.Invoke(new Action(delegate
{
fancyPopup.TextB.Text = "Done doing something...";
}));
}