2012-09-12 70 views
0

這種情況是在我的項目中我有一個bottom控件來啓動並檢查Windows服務並寫入在textBlock控件中的Windows服務的狀態。但是,問題是顯示「Windows服務正在運行」,然後「Windows服務已停止」,然後再次顯示「Windows服務正在運行」。文本塊控件的文本屬性從「A」更改爲「B」,然後再次更改爲「A」(線程相關)

這就像「A」到「B」,然後是「A」。

我希望它從「B」流到「A」,而不必在開始時輸入「A」。

private void btnStart_Click(object sender, RoutedEventArgs e) 
{ 
    if (ValidationHelpers.IsValid()) 
     StartService(); 
    else 
     _serviceControll.StopService(); 
} 

    private void StartService() 
     { 
      try 
      { 
       if (!_serviceIsStarted) 
       { 
        if (_serviceControll.StartService()) 
        { 

         var startThread = new Thread(new ThreadStart(CheckStartService)); 
         startThread.Start(); 

         statusTextBlock.Text = "Offline IM service is running"; 
         statusTextBlock.Foreground = Brushes.Black; 
        } 
        else 
        { 
         statusTextBlock.Text = "Offline IM service is stopped"; 
         statusTextBlock.Foreground = Brushes.Red; 
        } 
       } 
      } 
      catch (Exception exception) 
      { 

       statusTextBlock.Text = "Error in starting the Offline IM service " + exception.Message.ToString(); 
       statusTextBlock.Foreground = Brushes.Red; 
      } 
     } 



    private void CheckServiceStatus() 
    { 
     try 
     { 
      if (_serviceControll.CheckServiceStatus()) 
      { 
       statusTextBlock.Text = "Offline IM service is running"; 
       statusTextBlock.Foreground = Brushes.Black; 
       _serviceIsStarted = true; 
      } 
      else 
      { 
       statusTextBlock.Text = "Offline IM service is stopped"; 
       statusTextBlock.Foreground = Brushes.Black; 
       _serviceIsStarted = false; 
      } 
     } 
     catch (Exception exception) 
     { 
      statusTextBlock.Text = "Error in getting the Offline IM service status " + exception.Message.ToString(); 
      statusTextBlock.Foreground = Brushes.Red; 
     } 
    } 


    private void CheckStartService() 
     { 
      var set = true; 
      while (set) 
      { 
       if (_serviceControll.CheckServiceStatus()) 
       { 
        MessageBox.Show(@"Offline IM service is started", "Offline IM Configuration Manager", 
              MessageBoxButton.OK, MessageBoxImage.Information); 
        _serviceIsStarted = true; 
        set = false; 
       } 
      } 
      return; 
     } 


    private void DispatcherTimerTick(object sender, EventArgs e) 
    { 
     if (_serviceControll != null) 
     { 
      CheckServiceStatus(); 
     } 

    } 

} 

回答

0

一個這是不好的。

startThread.Start(); 

    statusTextBlock.Text = "Offline IM service is running"; 
    statusTextBlock.Foreground = Brushes.Black; 

僅僅因爲您啓動線程並不意味着該服務正在運行。

這個我不是在所有

if (_serviceControll.CheckServiceStatus()) 

CheckServiceStatus(),您已經張貼返回一個空

+0

這是另一個CheckServiceStatus ,, CheckServiceStatus不是_serviceControll.CheckServiceStatus() – tito11

+0

OK,但依然遵循不能僅僅因爲你啓動了線程而認爲服務正在運行 – Paparazzi

相關問題