3

拡張 WPF ツールキット BusyIndi​​cator を使用しています

私のXaml

<extToolkit:BusyIndicator Name="wait" IsBusy="False" Cursor="Wait" Grid.ColumnSpan="3" Margin="10,10,10,10"/>

私のコード:

private void esp_Click(object sender, RoutedEventArgs e)
{
    wait.IsBusy = true;

    // My work here make some time to finish

    wait.IsBusy = false;
}

しかし、決して表示されないので、関数の最後に MessageBox を作成し、BusyIndi​​cator が MessageBox の後に表示されるようにします。

私は試した

wait.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Send,
                       (Action)delegate
{
    wait.IsBusy = true;
});

しかし、私は何も得られませんでした!!! ここで解決できない問題はどこにありますか?

同様の質問を見つけましたが、インジケーターが表示されるのと同じ問題はありませんが、機能が完了した後です。

4

2 に答える 2

6

問題は、ディスパッチャーのスレッドですべての作業を実行していることです (これesp_Clickはイベント ハンドラーであると想定しています)。これは事実上、長いタスクを実行している間、UI が更新されていないことを意味します。

別のスレッドで作業を実行する必要があります-新しいスレッドを作成するか、スレッドプールを使用するか、タスクを作成します。作業開始前に~ 、作業終了後~にセットIsBusyしてください。別のスレッドから更新するときに使用する必要があります。truefalseDispatcher.BeginInvoke/Invokewait.IsBusy

サンプルコード:

private void LongRunningTask() 
{
   // your long running code

   // after you complete:
   Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Send,
                           (Action)delegate
    {
        wait.IsBusy = false;
    }); 
}

private void esp_Click(object sender, RoutedEventArgs e)
{
   wait.IsBusy = true; // either here, or in your long running task - but then remember to use dispatcher

   var thread = new Thread(LongRunningTask);
   thread.Start();

   // OR

   ThreadPool.QueueUserWorkItem(state => LongRunningState());

   // OR, in .NET 4.0

   Task.Factory.StartNew(LongRunningTask);
}

このソリューションはどちらも例外を処理しないことに注意してください。エラー処理を自分で追加する必要があります (または、最後のサンプルの場合はタスクの継続を使用します)。

于 2011-07-16T18:05:01.737 に答える
2

あなたはそれを行うことができますINotifyPropertyChanged

<extToolkit:BusyIndicator Name="wait" IsBusy="{Binding IsBusy}" Cursor="Wait" Grid.ColumnSpan="3" Margin="10,10,10,10"/>

そしてC#:

    /// <summary>
    /// The <see cref="IsBusy" /> property's name.
    /// </summary>
    public const string IsBusyPropertyName = "IsBusy";
    private bool _isBusy = false;

    public bool IsBusy
    {
        get
        {
            return _isBusy;
        }

        set
        {
            if (_isBusy != value)
            {
                _isBusy = value;
                RaisePropertyChanged(IsBusyPropertyName);                   
            }
        }
    }
于 2011-09-05T20:34:25.430 に答える