2011-01-18 11 views
0

私のアプリケーションには、サービスコントロールをラップするUserControlがあり、ユーザーに開始/停止/再起動サービス機能が公開されます。現時点で私の懸念が再開しています。少し時間がかかり、コントロールの再起動中の状態を反映させたい。これは私が私が私がアプリケーションに反映され、これらの変更が表示されていないデバッガをステップた場合でも、再起動ボタンをクリックハンドラクリックした後にボタンのプロパティを変更するWPF

private void RestartButton_Click(object sender, RoutedEventArgs e) 
{ 
    startStopButton.Visibility = Visibility.Hidden; 
    restartButton.Visibility = Visibility.Hidden; 
    statusTextBlock.Text = "Restarting..."; 

    Controller.Stop(); 
    Controller.WaitForStatus(ServiceControllerStatus.Stopped); 
    Controller.Start(); 
    Controller.WaitForStatus(ServiceControllerStatus.Running); 

    startStopButton.Visibility = Visibility.Visible; 
    restartButton.Visibility = Visibility.Visible; 

    statusTextBlock.Text = Controller.Status.ToString(); 
} 

のために持っているものおおよそです。私が紛失しているものでなければならない。また、私はそれらを隠すのではなくボタンを無効にしようとしましたが、どちらもうまくいきません。

答えて

2

UIスレッドですべてをやっているので、このコードが完了するまでUIは更新されません。あなたは、バックグラウンドスレッドで重い持ち上げをする必要があります。 BackgroundWorkerコンポーネントは、これは簡単です:実行はUIスレッドで起こっているためだ

private void RestartButton_Click(object sender, RoutedEventArgs e) 
{ 
    startStopButton.Visibility = Visibility.Hidden; 
    restartButton.Visibility = Visibility.Hidden; 
    statusTextBlock.Text = "Restarting..."; 

    var backgroundWorker = new BackgroundWorker(); 

    // this delegate will run on a background thread 
    backgroundWorker.DoWork += delegate 
    { 
     Controller.Stop(); 
     Controller.WaitForStatus(ServiceControllerStatus.Stopped); 
     Controller.Start(); 
     Controller.WaitForStatus(ServiceControllerStatus.Running); 
    }; 

    // this delegate will run on the UI thread once the work is complete 
    backgroundWorker.RunWorkerCompleted += delegate 
    { 
     startStopButton.Visibility = Visibility.Visible; 
     restartButton.Visibility = Visibility.Visible; 

     statusTextBlock.Text = Controller.Status.ToString(); 
    }; 

    backgroundWorker.RunWorkerAsync(); 
} 
+0

"簡単"は相対的なものになる可能性があります。何かがマルチスレッドの問題を抱えている可能性があります。 – Will

+0

私はすべての実装をバックグラウンドワーカーに移したいのですか、あるいはプロパティだけを変更するのでしょうか? – jlafay

+0

@jlafay:私の更新を参照してください。 –

0

を。 {}の間にUIスレッドが作業中であり、ボタンを更新できないため、あなたのボタンは更新されません。

関連する問題