User interface from service in .Net

  • Thread starter Thread starter Ken Allen
  • Start date Start date
K

Ken Allen

Can a .Net service be given a user interface? Even if I create it as a
'Windows Service', Visual Studio does not link in the
System.Windows.Forms namespace; and if I do so manually, then the
service hangs inside a WaitOne() inside the invoked delegate for a form
control update -- and the Form.Show() did nothing!

A friend of mine tells me that he was able to do this with VB6, but I
cannot figure out how to do this with C#/.Net -- is it possible?

-Ken
 
I would not recommend that you display a user interface from a .NET
service. If you really want to, you CAN allow it to interact with the
desktop by checking the "Allow Service to interact with the Desktop"
checkbox, which you can check if you select the "local system account"
in the "Log On" tab of the Local Services MMC applet.

By definition, Services should not be designed to display a graphical
user interface. If you need to display a UI, you can create a seperate
Windows Forms application, which interacts with the service through
remoting or some other IPC mechanism.

- Bennie
 
Yes, I know it is not recommended, but there are times when we need
this, even if only for limited periods of time.

Is there no way to enable this in the compilation.build phase? Must it
be enabled only after the service is installed? IN one case we have a
single project that contains multiple services, and we wanted to have a
single form displayed that included information from all of the services.

-ken
 
Look into ServiceController Class, which is used for control Wndows
Services.

You can build a standard Win Form App on Service Controller class to
monitor/control the Windows Service(s). Almost all books on Windows Service
have an example of Win Form app, using ServiceController class.
 
Create an installer class for your service project.

Override the Install method, like so:

public override void Install(IDictionary stateSaver) {
base.Install(stateSaver);
SetInteractWithDesktop();
}

private void SetInteractWithDesktop() {
RegistryKey fm;
ServiceType type;

fm = Registry.LocalMachine.OpenSubKey(
@"SYSTEM\CURRENTCONTROLSET\SERVICES\<your_service_name>, true );

type = ServiceType.InteractiveProcess |
ServiceType.Win32ShareProcess;
fm.SetValue( "Type", (int)type );

fm.Close();
}

That will modify the registry properly to check the Allow interaction
with desktop setting.

HTH.
Andy
 
Back
Top