events handling in windows services

[亡魂溺海] 提交于 2019-12-13 00:39:19

问题


I am new in creating windows services using C# framework 4.0, i have made a windows application that uses a DLL files to connect to a finger print attendance terminal.

but i am confused in how to convert windows app to windows services.

as i followed the tutorials i can run and install a simple services. but how to add new event in windows service

so first i add reference to my DLL file in project. second i added this code in the InitializeComponent method

    private void InitializeComponent()
    {  
        this.ServiceName = "MyService";
        AxBioBridgeSDK.AxBioBridgeSDKX MyBio = new AxBioBridgeSDK.AxBioBridgeSDKX();

        MyBio.OnAttTransaction += new AxBioBridgeSDK.IBioBridgeSDKXEvents_OnAttTransactionEventHandler(this.axBioBridgeSDKX1_OnAttTransaction); // this is the event handler function
    }

    #endregion
    private AxBioBridgeSDK.AxBioBridgeSDKX MyBio;
}

and in the OnStart() i add code to open connection with terminal. after i install the service and try to run it i get this error :

Windows could not start the "MyServiceName" service on Local Computer.

Error 1053: The service did not respond to the start or control request in a timely fashion.

Any help?


回答1:


Windows services are not meant to handle user interface events. They are designed to run in a separate memory space and with different credentials. Usually they don't require the user to be logged in.

The error you are getting is due the reason that your services start function is not properly behaving and the services is stopping immediately once you start it.

Debugging service requires some different techniques then debugging windows application. You need to install the service first and start it using service manager so that you can debug it.




回答2:


this could be any number of things. check the event log for failed .net execution, it may provide more information about why it failed.

some things to consider when going from user app to windows service.

  1. the account which runs the app is different, so security permissions may be a factor
  2. windows services to have user interaction, where a desktop app does.
  3. if a service does not start quickly enough windows will automatically abort the process because it thinks there was a failure.

in short desktop != windows service != web app. Each has there own context, purpose, strengths and weaknesses.



来源:https://stackoverflow.com/questions/12671458/events-handling-in-windows-services

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!