C # create a Windows Service

Source: Internet
Author: User
Use vs. Net to create a C # Windows Service In many applications, Windows services are required to operate databases, such
(1) Some very slow database operations, don't want to do it all at once, and want to do it slowly through regular service, such as regular database backup.
(2) Use Windows Service for host in. Net remoting
With vs.net, we can establish its Windows service in a few minutes, which is very simple.
The following are the steps
1. Create a project
2. Select a Windows service from the list of available project templates
3. The designer opens in design mode.
4. Drag a timer object from the component table in the toolbox to the design surface (Note: Make sure that timer is used from the component list rather than from the Windows form List)
5. Set the timer attribute. The interval attribute is 200 milliseconds (5 database operations are performed in 1 second)
6. Then fill in the function for this service
7. Double-click the timer and write some database operations in it.CodeFor example
Sqlconnection conn = new sqlconnection ("Server = Wagner. 0.0.1; database = test; uid = sa; Pwd = 275280 ");
Sqlcommand comm =-New sqlcommand ("insert into tb1 ('20140901', 11)", Conn );
Conn. open ();
Comm. executenonquery ();
Conn. Close ();
8. Set this serviceProgramSwitch to design view
9. Right-click the design view and select "add installer"

From: http://post.blog.hexun.com/imagine18/trackback.aspx? ArticleID = 6056177
10. Switch to the design view of the newly added projectinstaller.
11. Set the attributes of the serviceinstaller1 component:
1) servicename = My Sample Service
2) starttype = Automatic startup)
12. Set the attribute account = LocalSystem of the serviceprocessinstaller1 component.
13. Change the path to the bin \ debug folder of your project (if you compile in release mode, in the bin \ release folder)
14. Run the command "installutil mywindowsservice.exe" to register this service so that it can create an appropriate registration item. (Installutil is in the Windows Folder \ Microsoft. NET \ framework \ v1.1.4322)
15. Right-click "my computer" on the desktop and select "manage" to access the computer management console.
16. In the "services" section of "services and applications", you can find that your Windows services are included in the Service list.
17. Right-click your service and choose start to start your service.

Check if the database has five more records in one second.

C # Install and uninstall the batch processing code for Windows Services

Echo press any key to start installing the background services of the customer management platform...
Echo.
Pause
Echo.
Echo clears the original service items...
% SystemRoot % \ Microsoft. NET \ framework \ v2.0.50727 \ installutil/u d: \ service \ windowsservice1.exe> installservice. Log
Echo.
Echo cleaning is complete. Start to install the background service...
Echo.
% SystemRoot % \ Microsoft. NET \ framework \ v2.0.50727 \ installutil D: \ service \ windowsservice1.exe> installservice. Log
Install the echo service and start the service...
Net start crmservice> installservice. Log
Echo.
The echo operation is complete. Check the specific operation result in installservice. log.
Echo.
Pause

 

C # file access in Windows Service Projects

If the configuration file or other files are accessed in Windows service, you must configure the resource as a copy in the file attribute corresponding to the solution, or copy the resource if it is newer, the file will be copied under the generated directory.

In Windows Services, If you access a file, you can use an absolute path. If you use a relative path, and the generated service file is in the same directory, you need to access the files under this assembly.

Using System;
Using System. Collections. Generic;
Using System. text;

Using System. reflection;
Using System. xml;
Using System. IO;

Namespace Hdusearch
... {
Public Class Indexconfig
... {
Getvalue # Region Getvalue
Public   String Getvalue ( String Key)
... {
String Assemblyfilepath = Assembly. getexecutingassembly (). location;
String Assemblydirpath = Path. getdirectoryname (assemblyfilepath );

Xmldocument xmldoc =   New Xmldocument ();
Xmldoc. Load (assemblydirpath +   " \ Index. config " );

Xmlnodelist nodelist = Xmldoc. selectsinglenode ( " /Configuration/appsettings " ). Childnodes; // Obtain all the sub-nodes of the deleteworker Node
Foreach (Xmlnode XN In Nodelist) // Traverse all subnodes
... {
Xmlelement XE = (Xmlelement) xn; // Convert the subnode type to the xmlelement type
If (Xe. getattribute ( " Key " ). Indexof (key) ! =   - 1 )
... {
ReturnXe. getattribute ("Value");
Break;
}
}
Return   "" ;
}
# Endregion
}
}

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.