The case is that in my project i have a bottom control to start and check the Windows service and write the status of the Windows service in a textBlock control. But, the problem is it displays the “Windows service is running”, then “Windows service is stopped”, and then “Windows service is running” again.
This goes like “A” to “B” and then “A” again.
I want this to flow from “B” to “A” without having the “A” at the beginning.
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();
}
}
}
One this is not good.
Just because you start the thread does not mean the the service is running.
This I am not following at all
CheckServiceStatus() that you have posted returns a void