12

WPF アプリケーションのバックグラウンド ワーカーに奇妙な点があることに気付きました。

私が今達成しようとしているのは、BW が終了して別のスレッドを開始するまで待つことです。

次のコードを確認してください。

if (bw.IsBusy)
{
    bw.CancelAsync();

    System.Threading.ThreadStart WaitThread = 
        new System.Threading.ThreadStart(delegate() {
            while (bw.IsBusy)
            {
                System.Threading.Thread.Sleep(100);
            }

            bw.RunWorkerAsync();
        });

    System.Windows.Application.Current.Dispatcher.Invoke(
        System.Windows.Threading.DispatcherPriority.Normal,
        WaitThread);  // if I remove this line, bw fires RunWorkerAsyncEvent
}
else
{
    bw.RunWorkerAsync();
}

Dispatcher.Invoke を追加して bw がビジー状態になるまで待機することに注意してくださいRunWorkerAsyncCompleted。ただし、その行を削除すると、イベントが発生し、それは本当に奇妙です。

bw が終了するまでどのように待つことができますか?

4

5 に答える 5

30

これは「デッドロック」と呼ばれ、非常に一般的なスレッドの問題です。RunWorkerCompleted イベントが実行されるまで、BGW はビジー状態を止めることはできません。そのイベントはアプリのメイン スレッドで実行されます。メイン スレッドが他の処理でビジーでないときにのみ実行できます。ディスパッチャ ループ内で実行され、アイドル状態である必要があります。

しかし、メイン スレッドはアイドル状態ではなく、IsBusy が false を返すのを待っている while() ループ内でスタックしています。そのため、メイン スレッドが BGW の完了を待っているためにビジーであるため、イベントを実行できません。メイン スレッドがアイドル状態にならないため、BGW は完了できません。「致命的な抱擁」、別名デッドロック。

これを別の方法で行う必要があります。待つことはできません。BGW の別のインスタンスを作成するとします。または、待機後のコードを RunWorkerCompleted イベント ハンドラーに移動します。たとえば、ボタンの Click イベントによってアクティブ化された場合は、RunWorkerAsync() を呼び出すときに必ずボタンを無効にし、RunWorkerCompleted で再度有効にします。

于 2013-01-21T01:25:08.177 に答える
3

正しく理解したかどうかはわかりませんが、 BackgroundWorkerを再起動しようとしていると思います(つまり、ビジー状態の場合は、最初に停止してから再起動します)。これが必要な場合は、次のようにしてください。

クラスのどこかでこのデリゲートを宣言します。

    private delegate bool StateChecker();

このコードを使用して、BackgroundWorkerを再起動します。

    StateChecker stillWorking = () => { return bw.IsBusy; };

    if (bw.IsBusy)
    {
        bw.CancelAsync();
        while ((bool)this.Dispatcher.Invoke(stillWorking, null)) Thread.Sleep(15);
    }
    bw.RunWorkerAsync();
于 2013-01-21T06:58:40.653 に答える
2

これが完全で機能するコードです。

C# (ウィンドウのクラス)

 public partial class ForceSyncWindow : Window
    {
        BackgroundWorker backgroundWorker = new BackgroundWorker();

        public ForceSyncWindow()
        {
            InitializeComponent();

            ProgressBar1.Visibility = System.Windows.Visibility.Hidden;

            backgroundWorker.WorkerSupportsCancellation = true;

            // To report progress from the background worker we need to set this property
            backgroundWorker.WorkerReportsProgress = true;

            // This event will be raised on the worker thread when the worker starts
            backgroundWorker.DoWork += new DoWorkEventHandler(backgroundWorker_DoWork);

            // This event will be raised when we call ReportProgress
            backgroundWorker.ProgressChanged += new ProgressChangedEventHandler(backgroundWorker_ProgressChanged);

            backgroundWorker.RunWorkerCompleted += backgroundWorker_RunWorkerCompleted;
        }

        void backgroundWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            // First, handle the case where an exception was thrown. 
            if (e.Error != null)
            {
                MessageBox.Show(e.Error.Message);
            }
            else if (e.Cancelled)
            {
                // Next, handle the case where the user canceled  
                // the operation. 
                // Note that due to a race condition in  
                // the DoWork event handler, the Cancelled 
                // flag may not have been set, even though 
                // CancelAsync was called.
                ProgressBar1.Value = 0;
                // TODO LOG  = "Canceled";

            }
            else
            {
                // Finally, handle the case where the operation  
                // succeeded.
                // TODO LOG e.Result.ToString();

            }

            ProgressBar1.Value = 0;
            ProgressBar1.Visibility = System.Windows.Visibility.Hidden;

            // Enable the Synchronize button. 
            this.Synchronize.IsEnabled = true;

            // Disable the Cancel button.
            this.Cancel.IsEnabled = false;
        }

        // On worker thread so do our thing!
        void backgroundWorker_DoWork(object sender, DoWorkEventArgs e)
        {
              // Your background task goes here
                for (int i = 0; i <= 100; i++)
                {
                    if (backgroundWorker.CancellationPending == true)
                    {
                        e.Cancel = true;
                        break;
                    }
                    else
                    {
                        // Perform a time consuming operation and report progress.
                        // Report progress to 'UI' thread
                        backgroundWorker.ReportProgress(i);
                        // Simulate long task
                        System.Threading.Thread.Sleep(100); ;
                    }                              
                }            
        }

        // Back on the 'UI' thread so we can update the progress bar
        void backgroundWorker_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            // The progress percentage is a property of e
            ProgressBar1.Value = e.ProgressPercentage;
        }

        private void Synchronize_Click(object sender, RoutedEventArgs e)
        {
            ProgressBar1.Value = 0;
            ProgressBar1.Visibility = System.Windows.Visibility.Visible;

            // Disable the Synchronize button. 
            this.Synchronize.IsEnabled = false;

            // Enable the Cancel button.
            this.Cancel.IsEnabled = true;

            // Start the background worker
            backgroundWorker.RunWorkerAsync();
        }

        private void Cancel_Click(object sender, RoutedEventArgs e)
        {
            if (backgroundWorker.IsBusy)
            {
                // Cancel the background worker
                backgroundWorker.CancelAsync();
            }
        }
    }

XAML

<Window x:Class="MySyncManager.Views.ForceSyncWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

        Title="ForceSyncWindow" Height="300" Width="509" ResizeMode="NoResize" WindowStartupLocation="CenterScreen">
    <Grid>

        <Button Content="Synchronize" Name="Synchronize" HorizontalAlignment="Left" Margin="411,10,0,0" VerticalAlignment="Top" Width="75" Click="Synchronize_Click"/>
        <RichTextBox HorizontalAlignment="Left" Height="132" Margin="10,116,0,0" VerticalAlignment="Top" Width="476">

        </RichTextBox>
        <Button Content="Cancel" x:Name="Cancel" HorizontalAlignment="Left" Margin="411,40,0,0" VerticalAlignment="Top" Width="75" RenderTransformOrigin="0.508,2.154" Click="Cancel_Click"/>
        <ProgressBar Name="ProgressBar1" HorizontalAlignment="Left" Height="10" Margin="10,101,0,0" VerticalAlignment="Top" Width="476"/>

    </Grid>
</Window>
于 2014-07-31T16:29:48.963 に答える