.NET auto service - C # (1)

zhaozj2021-02-16  47

.NET auto service - C # (1)

Visualsw

In application development, it is often necessary to implement such a function: allowing the server to run a fixed program (or implement a fixed operation) at a certain time per day, such as backing up database data at 2:00 per day. To achieve such functions, we can use Windows Service.

Windows Service is a system automatic, unattended program (only in Windows NT, 2000, and XP operating systems), which can start running at system startup. Users can access Windows Service via Service Control Manager (SCM) Applet or some special service-control applications, enabling the service before being logged in to the system without users.

Before .NET appeared, writing a Windows service is VC , and Delphi can do things.

VB must use a third party control to do, and write special complications. With Microsoft®.NET Framework, we can easily create a Windows service by creating applications installed as a service.

design:

A Windows service program runs the specified program at the specified time according to the configuration in the configuration file.

Process:

Start Service à Read Profile à Start Timer

Timer timing trigger (such as 30 seconds per 30 second) à cycle requires running component time à time to à run specified program

write:

Create a Windows Service

Rename Server1.cs is schedulerserver.cs,

Double-click SchedulerServer.cs to open the design page, drag the Timer control from the toolbar's components.

Renamed Schedulertimer and set enabled to flash.

Note that the Timer control inside Components, the Timer control inside Windows Forms is not.

F7 browsing code can see the following code

When the server is started:

///

/// 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.

}

When the service is stopped:

///

/// stop this service.

///

protected override void onstop ()

{

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

}

Add a write log function to log the log:

Public Static Void WriteLog (String Strlog)

{

String strpath;

StrPath = system.environment.system.environment.systemdirectory;

StrPath = @ "/ schedulerServer.txt";

FILESTREAM FS = New FileStream (strpath, filemode.openorcreate, fileaccess.write);

Streamwriter M_StreamWriter = New StreamWriter (fs); m_streamwriter.basestream.seek (0, seekorigin.end);

m_streamwriter.writeLine (strlog);

M_StreamWriter.flush ();

M_StreamWriter.Close ();

fs.close ();

}

Automatic service configuration file

The configuration file is saved in the system directory, Windows 2000 is WinNT / System32, file named SchedulerServer.xml, stores all configuration information for automatic service, let's see the format of SchedulerServer.xml:

c: /autobackup.dll

02:00

d: /automail.dll

03:00

FilePath sets the path to the components that you need to run

Runtime settings need to run time

If you need to run multiple programs, just add nodes

Add a read configuration file function in the program:

Private bool readconf ()

{

Try

{

String strpath;

XMLDocument Xmldoc = new xmldocument ();

XMLNodelist Xmlnd;

StrPath = system.environment @ "/ schedulerserver.xml";

XMLDoc.Load (StrPath);

Xmlnd = Xmldoc.selectNodes ("SchedulerServer / AutoServer);

Arrconf = new string [2, xmlnd.count];

For (int i = 0; i

{

Arrconf [0, I] = XMLND [i] .selectsinglenode ("FilePath"). Innerxml.trim ();

Arrconf [1, I] = XMLND [i] .selectsinglenode ("runtime"). InnerXml.trim ();

}

Return True;

}

Catch (Exception E)

{

WriteLog (datetime.now.toString ());

WriteLog ("Read Configuration Error:");

WriteLog (e.tostring ());

Return False;

}

}

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

New Post(0)