情况是,在我的项目中,我有一个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();
}
}
}