Click here to Skip to main content
15,906,333 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
Hi,
I want to execute some codes in my windows service.i have tested these codes in a winform application and its worked successfully.but now when i install my windows service and i want to start it a message appears and tell me that my service starts and then stops because it has nothing to do!!!?

what is my mistake?
Note: I have a System.Timers.Timer in my service to execute some codes every 15 minutes.and i have installed my windows service application as an administrator.
Posted
Comments
Sergey Alexandrovich Kryukov 7-Sep-11 0:12am    
No way to figure this out without looking at your code. But I gave you general recommendation which will help you to detect a problem by yourself.
--SA

First, avoid timers by all means. Use a separate thread instead with System.Threading.Thread.Sleep. This is much more reliable and more straightforward to program.

Secondly, it's hard to see you problem without looking at your code. Services are harder to debug. Use System.Diagnostics.EventLog and log to the system event log you can open and view via the command:

%SystemRoot%\system32\eventvwr.msc /s


See http://msdn.microsoft.com/en-us/library/system.diagnostics.eventlog.aspx[^].

Another way is implementing most of you code in a separate assembly which you can use in you Windows Service and also in a regular interactive application, such as console application. However, remember that Windows Service is a very unusual host for running code: many aspects will look different. Create a very simple skeleton of the Windows Service first and make sure it works first, add some "meat" later step by step. Perform debug in both modes (interactive and Service) on regular basis.

Don't forget an important flag to use: the static property System.Environment.UserInteractive, see http://msdn.microsoft.com/en-us/library/system.environment.userinteractive.aspx[^]. Very useful.

—SA
 
Share this answer
 
v2
Comments
Mehdi Gholam 7-Sep-11 0:21am    
Nicely said, 5!
Sergey Alexandrovich Kryukov 7-Sep-11 0:24am    
Thank you, Mehdi.
--SA
M_Mogharrabi 7-Sep-11 0:28am    
thanks SA,but i did not understand how can i use UserInteractive property to solve my problem.is there any way instead of windows service?
Sergey Alexandrovich Kryukov 7-Sep-11 9:34am    
What do you mean by "any way instead of"?

UserInteractive property works for any application; it returns "true" if the code is running under Service, "false" otherwise. In this way, you can run the same piece of code under different host (interactive or not), but have an opportunity to make if behave a bit differently under if (System.Environment.UserInteractive) {/*...*/} else {/*...*/}. For example, you can write logs directly on screen if UserInteractive and to the system log if not. This is a powerful feature.
--SA
M_Mogharrabi 10-Sep-11 1:01am    
thanks a lot.I mean any thing that works like windows service and it does not have the problems of windows service?
To identify the problem, could you put try.. catch block

C#
try
{
....
}
catch (Exception ex)
{
... ex.Message....
}

It will catch the root cause for this service issue. From that, you can fix the problem which stops the service. My gut feeling is that there may be few setting/configuration issue in windows service definition.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 7-Sep-11 0:25am    
OP should to it on top of stack of each thread and log the full exception dump. My 5.
--SA
Hello friend this is same question like this..

please see this question and it will be solved if it is help full to you...

work with windows service[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900