Create a simple process for Windows services

Source: Internet
Author: User

1. First open VS2010 (or other version) to create a Windows service project

2. After the creation is switched to Code view, the code defaults to the OnStart and OnStop methods to perform service opening and service stop execution, the following code is explained in detail:

Note that the system time is selected, not the time in the WinForm.

Using System;

Using System.IO;

usingsystem.serviceprocess;

Using System.Text;

Usingsystem.timers;

Namespacetestservice

{

public partial class Service1:servicebase

{

Public Service1 ()

{

InitializeComponent ();

}

protected override Voidonstart (string[] args)

{

Service Open Execution Code

Startdosomething ();

}

protected override void OnStop ()

{

Service End Execution Code

}

protected override void OnPause ()

{

Service paused execution Code

Base. OnPause ();

}

protected override void OnContinue ()

{

Service Recovery Execution Code

Base. OnContinue ();

}

protected override void OnShutdown ()

{

System is about to close execution code

Base. OnShutdown ();

}

private void Startdosomething ()

{

System.Timers.Timer Timer = NewSystem.Timers.Timer (10000); Interval 10 seconds

Timer. AutoReset = true; boot start

Timer.  Enabled = false; Executes once, true when no 10S is executed.

Timer. Elapsed + = new Elapsedeventhandler (writesomething);

Timer. Start ();

}

private void Writesomething (Objectsource, System.Timers.ElapsedEventArgs e)

{

FileStream fs = null;

Try

{

FS = new FileStream ("D:/1.txt", FileMode.OpenOrCreate);

String strText = @ "//instantiate a file stream---> associated with Write file

FileStream fs = new FileStream (SF. FileName, FileMode.Create);

Instantiate a streamwriter--> associated with FS

StreamWriter SW = new StreamWriter (FS);

Start writing

Sw. Write (This.textBox1.Text);

emptying buffers

Sw. Flush ();

Close the stream

Sw. Close ();

Fs. Close (); ";

Get byte array

byte[] data = new UTF8Encoding (). GetBytes (StrText);

Start writing

Fs. Write (data, 0, data. Length);

Empties the buffer, closes the stream

Fs. Flush ();

Fs. Close ();

Fs. Dispose ();

}

Catch

{

}

Finally

{

if (fs! = NULL)

{

Fs. Close ();

Fs. Dispose ();

}

}

}

}

}

3. Then switch to Design view, right-click on the "Add Installer" in circle

4. Select the first control, click F4, switch to the property view on the right, change the account property in the Properties view to LocalService (Local service)

5. Select the second control above, click F4, and switch to the property view on the right. Change ServiceName to your own favorite service name, remember not to conflict with the system, starttype default to Manual, you may change to automatic

(Automatic) or disabled (Disabled)

6. Compile the project, then win+r enter cmd into the command window. To find InstallUtil.exe in the directory corresponding to the. NET version, my project uses. NET 4.0, so the path is C:\WINDOWS\Microsoft.NET\Framework\v4.0.30319

7.installutil.exe corresponds to the. NET version directory diagram, as follows

8. Then win+r enter cmd into the command window.

9.

Method Delete Service: Direct registry edit open Registry Editor and locate the following key value:
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services General Service will show a key in the same name here, directly delete the relevant keys can be.

Create a simple process for Windows services

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.