0

I have simple application, with hidden form and not displaying on taskbar (settings of form), with one purpose - show tray icon, when service is running.

Service is implemented by me as well, and uses duplex callback to send message to this application. When it is about to stop, it sends message to applicaion, so it hides tray icon. The thing is, that i need to show icon, when service is up again. For now I'm doing this with WaitForService with no timeout.

if (sc.Status != ServiceControllerStatus.Running)
    sc.WaitForStatus(ServiceControllerStatus.Running);
_notifyIcon.Visible = true;

This works ok - code suspends its execution, until service is up again.

But there is one problem - if application waits over some time, it is marked as "Not responding", it appears in taskbar, and some sort of small window appears. So, is there any alternative to avoid that? Either some workaround for WaitForStatus, or different method for monitoring service status.

lentinant
  • 792
  • 1
  • 10
  • 36

1 Answers1

1

It seems that ServiceController.WaitForStatus blocks the UI thread.

To avoid this, you have multiple choices. I would try this

Thread t = new Thread(() =>
{
    sc.WaitForStatus(ServiceControllerStatus.Running);
    // Add something to do after the status updates
});
t.Start();

But remember that you have to use Form.Invoke to interact with the application UI in another thread.

this.Invoke(new Action(() => 
{
    _notifyIcon.Visible = true;
}));
Sorashi
  • 931
  • 2
  • 9
  • 32