2

データベースがローカルではないため、データの読み込みに時間がかかる可能性があるため、ユーザーがコントロールから選択するとすぐに読み込みポップアップを表示したい wpf フォームを取得しました。ポップアップウィンドウのスレッドを作成するところまで、すべてがうまくいきました。

これは私が私のスレッドを作成する場所です:

public void Start()
    {

         if (_parent != null)
             _parent.IsEnabled = false;

         _thread = new Thread(RunThread);

         _thread.IsBackground = true;
         _thread.SetApartmentState(ApartmentState.STA);
         _thread.Start();

         _threadStarted = true;
         SetProgressMaxValue(10);

         Thread th = new Thread(UpdateProgressBar);
         th.IsBackground = true;
         th.SetApartmentState(ApartmentState.STA);
         th.Start();
    }

そしてスレッドメソッド:

private void RunThread()
    {

        _window = new WindowBusyPopup(IsCancellable);
        _window.Closed += new EventHandler(WaitingWindowClosed);
        _window.ShowDialog();
    }

今、私がこのエラーを取得する瞬間:

親の Freezable とは異なるスレッドに属する DependencyObject は使用できません。

どんな助けでも大歓迎です:)

4

2 に答える 2

0

親の Freezable とは異なるスレッドに属する DependencyObject は使用できません。

このエラーは、(ポップアップ ウィンドウを表示するために使用している) STA スレッドの別のスレッドで作成された (UIElement タイプの) リソースを使用しようとしているために発生します。

あなたの場合、2番目のスレッドThread th = new Thread(UpdateProgressBar);のように見えます。WindowBusyPopupで UI を操作しようとしています。ポップアップが別のスレッドによって所有されているため、この例外が発生しています。

考えられる解決策:(私が見るように、関数UpdateProgressBarの実装は表示されていません)

private void UpdateProgressBar()
{
if(_window != null) /* assuming  you declared your window in a scope accesible to this function */
_window.Dispatcher.BeginInvoke(new Action( () => {
// write any code to handle children of window here
}));
}
于 2012-06-20T12:04:45.123 に答える
0

フォームの Dispatcher プロパティを使用してみてください。Dispatcher.BeginInvoke(...)

または、BackgroundWorkerクラスを使用します。ReportProgress() というメソッドがあり、進行状況のパーセンテージを報告します。これにより、プログレスバーまたは何かの値を更新できるときに、ProgressChanged イベントが発生します...

于 2012-01-30T10:12:21.477 に答える