C # Starting a Windows Form application from a service (and in Vista)

I am writing a C # application that should run as a service, but also have user interaction. I understand that services do not have a user interface, etc. Therefore, I divided my program into a Windows form application and a service that can communicate with each other.

The problem I am facing is that I need a service to make sure that the Windows form application is always running and reloads it if it is not. I can determine if it is running and restart it with the following code in Windows 2000 / XP:

System.Diagnostics.Process.Start("ExePath"); 

but in Vista, it launches a new process as a local / system process that is invisible to the user. Has anyone got around this? Is there a way to determine which user is currently logged in and start a new process as that user? At the moment, I donโ€™t need to consider fast user switching. Something - nothing - basic would be enough.

I would be grateful for any help or advice that you have on this issue.

I need to clarify that when installing the service, I set the "Allow the service to interact with the desktop" option. This allows you to work with 2000 / XP. However, Vista still has the above problem.

+7
c # windows-vista winforms windows-services
source share
8 answers

The general idea of โ€‹โ€‹this kind of thing is that if the user needs to interact with the service, they must run a separate application. If you want to help them, you can configure this standalone application to run using windows by placing a shortcut in the start menu. You can also create disaster recovery in your application so that it can automatically restart.

You should not rely on monitoring application forms, what if no one is logged in? What if several people are logged in? It just gets confused doing something this way.

Having a service just sitting and broadcasting to listeners is the way to go. When the forms application starts, it can notify the service that it wants to listen to events.

+13
source share

See the question: How can a Windows service run a GUI application? . It solves the same question from C / C ++ (short answer: CreateProcessAsUser), but the answer remains valid (with some P / Invoke) for C #.

+3
source share

In this case, you will have to have a third monitoring process, which determines if the program will work and will restart it in this case.

However, you end up with an insoluble problem here, since you need to monitor the process of the monitor so that it does not close, etc. etc. etc. etc.

You might want to rethink this approach.

+2
source share

This is a difficult situation. As mentioned in several places, if you must have a user interface, then technically you should not use the service. Afterall, services are started without the participation of a user, even a user logging in. If no one is logged in, you cannot have a user interface.

Usually, when I need a service to connect with the outside world, there are two things that I choose. I can either put an entry in the event log, or I can send a message to the queue.

In your case, I would use a queue. When a user logs in, you can automatically launch the application for those who control the queue. If the application is launched when a message is received, they are also warned about this. However, if the user closes the application, then the same thing happens ... they will not know.

+1
source share

Firstly, the quick answer: does the option โ€œAllow the service to interact with the desktopโ€ (service โ†’ Properties โ†’ LogOn) allow or specify an account, what do you want? If so, both of them can be configured in your service installer class.

Like others, I suspect there is a better approach to this, and one of the following is true: The code inside the service can be included in the winforms application (possibly running in the background thread) and added to Windows startup. Both will work. The winforms application can simply listen to the service when it is turned on, and it does not need to be run from the service. Or similarly, an application can be added to the launch.

+1
source share

In order for your service to run the application as a user (which is similar to what you are trying to do), you need to do the following:

 System.Security.SecureString ss = new System.Security.SecureString(); foreach (char c in password) ss.AppendChar(c); System.Diagnostics.Process proc = Process.Start(path, arguments, username, ss, domain); 

Where:

  • path = full path (including file name) of the executable file.
  • arguments = argument string (use empty string none)
  • username = The name of the user account on your server / computer
  • domain = your network domain (if you are using a network account - empty if not)

In addition, for your service to have permission to run the application, it must also work as a service. To do this, you need to add these lines to the service installer class:

 serviceProcessInstaller.Account = ServiceAccount.User; serviceProcessInstaller.Username = "yourdomain\\yourAccountName"; //Or just "AccountName" for local accounts.. serviceProcessInstaller.Password = "yourPassword"; 
+1
source share

In Windows 2000 and XP, there is an option (check box) on the Login tab of the service properties window to allow the service to interact with the desktop. I believe that this is what you are looking for. I just wrote a quick service in VB.NET with the open Process.Start process ("calc.exe") and a Windows calculator.

I'm not sure that 100% of this works the same in Vista.

0
source share

It looks like you may not need half of his work as a service (unless the requirement of higher privileges is required), since your service will need to cope when there is no interactive user logging in.

0
source share

All Articles