Start stop Service from Form App c#

Add a reference to System.ServiceProcess.dll. Then you can use the ServiceController class. // Check whether the Alerter service is started. ServiceController sc = new ServiceController(); sc.ServiceName = “Alerter”; Console.WriteLine(“The Alerter service status is currently set to {0}”, sc.Status.ToString()); if (sc.Status == ServiceControllerStatus.Stopped) { // Start the service if the current status is stopped. Console.WriteLine(“Starting the … Read more

How can I set up .NET UnhandledException handling in a Windows service?

The reason that the UnhandledException event on the current AppDomain does not fire is how services are executed. User sends a Start command from the Windows Service Control Manager (SCM). The command is received by the framework’s ServiceBase implementation and dispatched to the OnStart method. The OnStart method is called. Any exception which is thrown … Read more

Windows Service not appearing in services list after install

The most important part of the article you linked, is here To add a custom action to the setup project 1.In Solution Explorer, right-click the setup project, point to View, then choose Custom Actions. The Custom Actions editor appears. 2.In the Custom Actions editor, right-click the Custom Actions node and choose Add Custom Action. The … Read more

Logoff interactive users in Windows from a service

You could use following P/Invoke calls to achieve this. Below sample works only with Admin Rights [DllImport(“wtsapi32.dll”, SetLastError = true)] static extern bool WTSLogoffSession(IntPtr hServer, int SessionId, bool bWait); [DllImport(“Wtsapi32.dll”)] static extern bool WTSQuerySessionInformation( System.IntPtr hServer, int sessionId, WTS_INFO_CLASS wtsInfoClass, out System.IntPtr ppBuffer, out uint pBytesReturned); [DllImport(“wtsapi32.dll”, SetLastError = true)] static extern IntPtr WTSOpenServer([MarshalAs(UnmanagedType.LPStr)] String … Read more

Installing Windows Service programmatically

You can install the service by adding this code (in the program file, Program.cs) to install itself when run from the commandline using specified parameters: /// <summary> /// The main entry point for the application. /// </summary> static void Main(string[] args) { if (System.Environment.UserInteractive) { if (args.Length > 0) { switch (args[0]) { case “-install”: … Read more

“Automatic” vs “Automatic (Delayed start)”

In short, services set to Automatic will start during the boot process, while services set to start as Delayed will start shortly after boot. Starting your service Delayed improves the boot performance of your server and has security benefits which are outlined in the article Adriano linked to in the comments. Update: “shortly after boot” … Read more

tech