I implemented a windows form application and would like to run it automatically after installing it as a service. (run the exe file as a service).
In my form, it will be two options (Install the service option and uninstall).
So, my application will be installed as a service, if the user wants to install that as a service, else the user can run it once there is a need for that.
Is this possible to achieve by writing a code in my windows form application project, or it needs to configure a service first by creating a Windows Service project?
Eline SamiPosted Feb 18, 2016, 6:11 AM
Eline SamiPosted Feb 17, 2016, 4:41 AM
Hello,
I created the Windows service and it runs fine. It can be installed and uninstalled as well.
but how to link the service to the windows form application? In other words. In my Windows form appliciton there is an option to install, but don't know how to call the service and install it. is it possible to share a snippet of code (as an example)?
I found the following, but don't know how to pass the servie name and how to initialize a new instance from the service:
ServiceController sc = new ServiceController(SERVICENAME);
switch (sc.Status)
{
case ServiceControllerStatus.Running:
return "Running";
case ServiceControllerStatus.Stopped:
return "Stopped";
case ServiceControllerStatus.Paused:
return "Paused";
case ServiceControllerStatus.StopPending:
return "Stopping";
case ServiceControllerStatus.StartPending:
return "Starting";
default:
return "Status Changing";
}
private void InstallService()
{
System.ServiceProcess.ServiceBase[] ServicesToRun;
// Change the following line to match.
ServicesToRun = new System.ServiceProcess.ServiceBase[] { new MyNewService() };
System.ServiceProcess.ServiceBase.Run(ServicesToRun);
}
Francis SusaimichaelPosted Feb 17, 2016, 2:09 AM
Nishant MittalPosted Feb 17, 2016, 1:31 AM