Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Windows Service is not Working

Tags:

c#

.net

I had made a windows service in visual studio 2008 in C#. inside the service i had written only single line code

 try
 {
     System.Diagnostics.Process.Start(@"E:\Users\Sk\Desktop\category.txt");
 }
 catch { }

then i add the project installer & change the serviceProcessInstaller1 Account property as local system Also change the serviceInstaller1 start type property as Automatic. then i build the project.it was successful. after that i add another project that was setup project.i had added primary project output & i had added the custom action as "Primary output from DemoWindowsService (Active)".then built the setup.setup was build successfully.then i install the setup & then went to services start the service.service stated properly but it was not performing the task. i had checked the path is correct & also i tried to do

System.Diagnostics.Process.Start(@"E:\Windows\system32\notepad.exe") but still result is same.i tried a lot but not getting the answer.

like image 582
PrateekSaluja Avatar asked Apr 05 '10 07:04

PrateekSaluja


2 Answers

Windows services cannot interact with the desktop by default, and Notepad is an interactive application.

If you open Task Manager, I suspect you will see the notepad.exe process running after you started your service, and that your service is doing what you think, it's just not visible.

You can change a service to interact with the desktop by right-clicking it in the Services MMC snap-in (Start -> Run -> Services.msc), and checking the Allow service to interact with desktop option on the Log On tab.

Relying on this option is not recommended though, as Microsoft have said they will remove it in later versions of Windows.

It makes sense if you think about it, since a Windows service is meant to be usable even if no-one is logged on to the computer (e.g. there is no "desktop" to interact with).

like image 104
Leon Breedt Avatar answered Nov 15 '22 17:11

Leon Breedt


If it's not working, then maybe it's because an exception was thrown. However, since you're hiding all exceptions with your try/catch block, you'll never know what's wrong.

Get rid of the try/catch block, and see if that helps you learn what's wrong.

like image 29
John Saunders Avatar answered Nov 15 '22 18:11

John Saunders