日韩黑丝制服一区视频播放|日韩欧美人妻丝袜视频在线观看|九九影院一级蜜桃|亚洲中文在线导航|青草草视频在线观看|婷婷五月色伊人网站|日本一区二区在线|国产AV一二三四区毛片|正在播放久草视频|亚洲色图精品一区

分享

Howto:WriteServicesProgrammatically|Microsof...

 蘭亭文藝 2019-08-28

How to: Write Services Programmatically

If you choose not to use the Windows Service project template, you can write your own services by setting up the inheritance and other infrastructure elements yourself. When you create a service programmatically, you must perform several steps that the template would otherwise handle for you:

  • You must set up your service class to inherit from the ServiceBase class.

  • You must create a Main method for your service project that defines the services to run and calls the Run method on them.

  • You must override the OnStart and OnStop procedures and fill in any code you want them to run.

To write a service programmatically

  1. Create an empty project and create a reference to the necessary namespaces by following these steps:

    1. In Solution Explorer, right-click the References node and click Add Reference.

    2. On the .NET Framework tab, scroll to System.dll and click Select.

    3. Scroll to System.ServiceProcess.dll and click Select.

    4. Click OK.

  2. Add a class and configure it to inherit from ServiceBase:

    C#
    public class UserService1 : System.ServiceProcess.ServiceBase {}
    Public Class UserService1    Inherits System.ServiceProcess.ServiceBaseEnd Class
  3. Add the following code to configure your service class:

    C#
    public UserService1() { this.ServiceName = 'MyService2'; this.CanStop = true; this.CanPauseAndContinue = true; this.AutoLog = true;}
    Public Sub New()    Me.ServiceName = 'MyService2'    Me.CanStop = True    Me.CanPauseAndContinue = True    Me.AutoLog = TrueEnd Sub
  4. Create a Main method for your class, and use it to define the service your class will contain; userService1 is the name of the class:

    C#
    public static void Main(){ System.ServiceProcess.ServiceBase.Run(new UserService1());}
    Shared Sub Main()    System.ServiceProcess.ServiceBase.Run(New UserService1)End Sub
  5. Override the OnStart method, and define any processing you want to occur when your service is started.

    C#
    protected override void OnStart(string[] args){ // Insert code here to define processing.}
    <code name="vbradconservice#10" data-author-content="protected overrides sub cg(byval args() as string)    " insert="" code="" here="" to="" define="" processing.end="" sub'="">Protected Overrides Sub OnStart(ByVal args() As String)    ' Insert code here to define processing.End Sub
  6. Override any other methods you want to define custom processing for, and write code to determine the actions the service should take in each case.

  7. Add the necessary installers for your service application. For more information, see How to: Add Installers to Your Service Application.

  8. Build your project by selecting Build Solution from the Build menu.

    Note

    Do not press F5 to run your project — you cannot run a service project in this way.

  9. Create a setup project and the custom actions to install your service. For an example, see Walkthrough: Creating a Windows Service Application in the Component Designer.

  10. Install the service. For more information, see How to: Install and Uninstall Services.

    本站是提供個(gè)人知識管理的網(wǎng)絡(luò)存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約