How to create a Windows Service with C #

xiaoxiao2021-03-13  198

Create Windows Service in .NET, it is very simple. You can make a simple Windows Service by following the steps below.

1. First select Windows Service when you create a project, so .NET automatically generates a Windows Service's framework;

2. Complete the corresponding event of Windows Service, mainly two events onStart and onstop, and the substantially code is as follows:

Using system;

Using system.collections;

Using system.componentmodel;

Using system.data;

Using system.diagnostics;

Using system.serviceProcess;

Using system.io;

Using system.threading;

Namespace WinsDemo

{

Public Class WinsDemo: System.ServiceProcess.serviceBase

{

///

/// Required Designer Variable.

///

Private system.componentmodel.Container Components = NULL;

PRIVATE BOOL BLNSTOPTHREAD;

PRIVATE Thread twmain;

Public winsdemo ()

{

// this call is required by the Windows.Forms Component Designer.

InitializationComponent ();

// Todo: add anyinitization after the initcomponent call

}

// the main entry point for the process

Static void

Main

()

{

System.ServiceProcess.serviceBase [] ServicesTorun;

// More Than One User Service May Run with the Same Process. To Add

// another service to this process, Change the Following Line To

// CREATE A Second Service Object. for example,

//

ServicesTorun = new system.serviceProcess.serviceBase [] {new winsdemo ()};

System.ServiceProcess.serviceBase.Run (ServiceStorun);

}

///

/// Required Method for Designer Support - Do Not Modify

/// The contents of this method with the code editor.

///

Private vidinitiRizeComponent ()

{

//

// WinsDemo

//

THIS.CANPAUSEANDCONTINUE = True;

THIS.ServiceName = "mytest";

}

///

/// Clean Up Any Resources Being Used.

///

Protected Override Void Dispose (BOOL Disposing) {

IF (Disposing)

{

IF (Components! = NULL)

{

Components.dispose ();

}

}

Base.dispose (Disposing);

}

///

/// set Things in Motion So your service can do its work.

///

protected override void onstart (String [] ARGS)

{

// Todo: add code here to start your service.

THDMAIN = New Thread (New ThreadStart (WriteLog);

THDMAIN.START ();

}

///

/// stop this service.

///

protected override void onstop ()

{

// Todo: Add code Here to Perform Any Tear-Down Necessary to Stop Your Service.

BLNSTOPTHREAD = TRUE;

THDMAIN.JOIN ();

}

protected override void onpause ()

{

THDMAIN.SUSPEND ();

}

Protected Override void oncontinue ()

{

THDMAIN.RESUME ();

}

protected void writelog ()

{

Streamwriter mywriter = null;

DO

{

Process.start ("Net", "Send Localhost YourValue);

Try

{

MyWriter = New StreamWriter ("c: //mylog.txt", true);

Mywriter.writeline (DateTime.now.toString ());

MyWriter.Close ();

}

CatCh {};

Thread.sleep (5000);

} while (blnstopthread == false);

}

}

}

Note: In order to make yourself better identify yourself Windows Service, it is recommended to modify the name of Service in InitializationComponent.

3. In order to make it written in the system, it is not enough to depends on the above steps; next, add Service INSTALLER to the current engineering, set the starting state after the service installation, the code is as follows:

Using system;

Using system.collections;

Using system.componentmodel;

Using system.configuration.install;

Using system.serviceProcess;

Namespace WinsDemo

{

///

/// Summary Description for WinsDemoins.

///

[Runinstaller (TRUE)]

Public Class WinsDemoins: System.Configuration.install.installer

{

///

/// Required Designer Variable.///

Private system.componentmodel.Container Components = NULL;

Private ServiceInstaller ServiceInstaller;

Private ServiceProcessInstaller ProcessInstaller;

Public winsdemoins ()

{

// this call is required by the designer.

InitializationComponent ();

// Todo: add anyinitization after the initcomponent call

ProcessInstaller = New ServiceProcessInstaller ();

ServiceInstaller = New ServiceInstaller ();

// Service Will Run Under System Account

ProcessInstaller.account = serviceAccount.localsystem;

// service will have start type of manual

ServiceInstaller.startType = ServiceStartMode.Automatic;

ServiceInstaller.ServiceName = "mytest";

Installers.Add (ServiceInstaller);

Installers.Add (ProcessInstaller);

}

#Region Component Designer Generated Code

///

/// Required Method for Designer Support - Do Not Modify

/// The contents of this method with the code editor.

///

Private vidinitiRizeComponent ()

{

Components = new system.componentmodel.container ();

}

#ndregion

}

}

4. Complete the above steps, the part of the code is completed, compile into an executable, and then use the .NET's service installation tool, in the DOS window, type "installutil yourService.exe", which is OK, reverse If you want to uninstall the service, you can add a parameter, ie "Installutil / u YourService.exe". Note that there is no .NET's path does not exist in environment variables. It may be directly executed. I hope to find the directory where "installutil.exe" exists, roughly "/Windows/MICROSOFT.NET/framework/v1.1.4322" Under contents.

As for the deployment of SERVICE, due to .NET written program, the running environment must be installed .NET Framework, so you must install the .NET running environment when you write your own service in other machines.

转载请注明原文地址:https://www.9cbs.com/read-128980.html

New Post(0)