2011-01-18 2 views
0

내 응용 프로그램에는 사용자에게 시작/중지/다시 시작 서비스 기능을 제공하기 위해 ServiceController를 래핑하는 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 스레드가 작업을 수행 중이며 버튼을 업데이트 할 수 없기 때문에 버튼이 업데이트되지 않습니다.

관련 문제