Windows service with timer

前端 未结 3 1538
迷失自我
迷失自我 2020-11-27 05:18

I have created a windows service with timer in c#.net. it works fine while i debug/build the project in visual studio but it does not perform its operation after installatio

3条回答
  •  野趣味
    野趣味 (楼主)
    2020-11-27 05:31

    You need to put your main code on the OnStart method.

    This other SO answer of mine might help.

    You will need to put some code to enable debugging within visual-studio while maintaining your application valid as a windows-service. This other SO thread cover the issue of debugging a windows-service.

    EDIT:

    Please see also the documentation available here for the OnStart method at the MSDN where one can read this:

    Do not use the constructor to perform processing that should be in OnStart. Use OnStart to handle all initialization of your service. The constructor is called when the application's executable runs, not when the service runs. The executable runs before OnStart. When you continue, for example, the constructor is not called again because the SCM already holds the object in memory. If OnStop releases resources allocated in the constructor rather than in OnStart, the needed resources would not be created again the second time the service is called.

提交回复
热议问题