最近、WPF コードをリファクタリングしたところ、DispatcherTimer が起動しなくなりました。ここで他の同様の投稿を確認しましたが、それらはすべて、私が試したディスパッチャースレッドセットが間違っていることに問題があるようでした...
私のコードは次のようになります。
class MainWindow : Window
{
private async void GoButton_Click(object sender, RoutedEventArgs e)
{
Hide();
m_files = new CopyFilesWindow();
m_files.Show();
m_dispatcherTimer = new DispatcherTimer();
m_dispatcherTimer.Tick += dispatcherTimer_Tick;
m_dispatcherTimer.Interval = new TimeSpan(0, 0, 0, 0, 250);
m_dispatcherTimer.Start();
await SomeLongRunningTask();
m_files.Hide();
Show();
}
(現在のクラスはメインの Window オブジェクトで、ファイルのコピー中は非表示にします。CopyFilesWindow は、私が変更したコントロールを含む単純な Xaml ウィンドウです。CopyFilesWindow 自体はまったく何もしません。)
基本的に、私は長時間実行されるタスク (大量の大きなファイルをコピーする) を待ちます。私の DispatcherTimer は、dispatcherTimer_Tick の進行状況を更新することになっています。ただし、その関数にブレークポイントを設定しましたが、ヒットしません。
また、次のようにコンストラクターを使用して Dispatcher を設定しようとしました。
m_dispatcherTimer = new DispatcherTimer(DispatcherPriority.Normal, m_files.Dispatcher);
m_dispatcherTimer = new DispatcherTimer(DispatcherPriority.Normal, this.Dispatcher);
しかし、これらのどちらも動作を変更しません...それでも起動しません。
ここで何が間違っていますか?