0

情况是,在我的项目中,我有一个bottom控件来启动和检查 Windows 服务并将 Windows 服务的状态写入textBlock控件中。但是,问题是它显示“Windows 服务正在运行”,然后“Windows 服务已停止”,然后再次显示“Windows 服务正在运行”。

这就像“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();
        }

    }

}
4

1 回答 1

0

一这不好。

   startThread.Start();

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

仅仅因为您启动线程并不意味着服务正在运行。

我根本不关注这个

if (_serviceControll.CheckServiceStatus())

您发布的 CheckServiceStatus() 返回 void

于 2012-09-12T22:34:34.293 回答