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)
-- Note:[Recommended] [blood lessons] C # Timer controls in windows service programs
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 to generate the event "private void timerreceivelapsed (object sender, System. timers. elapsedEventArgs e ){...} "method, and then write some database operation code in it, such
Code
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 ();
Appendix: it can also be written in Notepad
Code
FileStream fs = new FileStream (@ "d: \ My Sample Service.txt", FileMode. OpenOrCreate, FileAccess. Write );
StreamWriter m_streamWriter = new StreamWriter (fs );
M_streamWriter.BaseStream.Seek (0, SeekOrigin. End );
M_streamWriter.WriteLine ("My Sample Service:" + DateTime. Now. ToString () + "\ n ");
M_streamWriter.Flush ();
M_streamWriter.Close ();
Fs. Close ();
8. Switch the service program to the design view.
9. Right-click the design view and select "add installer"
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. when the service is running, the Timer control is enabled, that is, timer1.Start () is added to the OnStart event ();
Protected override void OnStart (string [] args)
{
Timer1.Start ();
}
14. Change the path to the bin \ Debug folder of your project (if you compile in Release mode, in the bin \ Release folder)
15. Execute the command "installutil.exe 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)
--Installation tips:The Installation File installutil.exe cute is stored in the directory of your mywindowsservice.exejust. You can use the mouse to place your mywindowsservice.exee.onto the "“mywindowsservice.exe" icon to quickly install the installation. You do not need to enter related commands in the command console.
16. Right-click "my computer" on the desktop and select "manage" to access the computer management console.
17. In the "services" section of "services and applications", you can find that your Windows services are included in the Service list.
18. Right-click your service and choose start to start your service.
Check if the database has five more records in one second.
Note that:
If you modify this service and the path remains unchanged, you do not need to register the service again,
If the plugin changes, uninstall the installutil.exe-u parameter and reinstall the service.
In addition, the Windows Service has no interface. Do not output some information in the controlled output mode. You can only add one EventLog and write logs through writeentry.
For more information about how to use the Windows service for a remote service, see
Http://msdn.microsoft.com/library/default.asp? Url =/library/en-US/dnnetsec/html/secnetht15.asp
Download instance
Feedback
# It's too easy on the first floor [main poster] k
View reply reference
# A color on the second floor will be mad at my machine.
Recently, I don't know what's going on. When Account = LocalSystem, the service cannot be started. It can only be set to./administrator.
Then, an EventLog is added. It cannot be written through WriteEntry.
You cannot delete Messaging. MessageQueue...
Is it true that I want to reinstall the machine?
View reply reference
# On the third floor, 60.176.53. *-10-18 tianqiu [unregistered users] I did it again. I don't know which step did the mistake. When executing the command "installutil.exe MyWindowsService.exe", the system prompts an error: An exception occurred during the "installation" phase. System. ComponentModel. Win32Exception: the specified service already exists ............. Affected parameters: assemblypath = e: \ windowserver \ bin \ debug \ mywindowserver.exe
Logfile = e: \ windowserver \ bin \ debug \ mywindowserver. InstallLog
............
Transaction processing installation completed
Installation failed. rollback has been executed.
Let me know about prawns.
Reply to reference
# Lovecherry first uninstalls the service by using-u or by another service name on the 4th floor []. The service already exists
View reply reference
#218.108.1. * [unregistered users] Thank you very much. The problem is finally solved. However, after I created another Windows service program, I realized that these two services could not be stored in parallel. when there was a service in the service, the system then executed the command "installutil.exe", prompting that the specified service already exists. They generate different exe file names and paths ." Or change the service name ", which is the service name. Ask again
Reply to reference
#6 floor [main poster] lovecherry 11. Set the attribute of serviceInstaller1 component:
1) ServiceName = My Sample Service
2) StartType = Automatic startup)
1) The service name cannot be the same
View reply reference
#7 floor-12-12 hyde TO: Tian Qiu
It seems to be related to CLSID or PUID.
View reply reference
# On the 8th floor, 222.66.20.*2006-01-24 [unregistered users]: "Images cannot be verified"
What's going on?
Reply to reference
# The ninth floor was written with a smile at a.m. on May 25,. I have added my favorites, but please write the SQL correctly.
View reply reference
#10 floor 60.191.36.*2006-04-12 and [unregistered users] a dialog box is displayed, requiring you to enter the user name and password
The following error is prompted after I enter it. It is not installed.
System. ComponentModel. Win32Exception
Reply to reference
# Setting serviceProcessInstaller1 attribute Account = LocalSystem on the 11 th floor [main poster] lovecherry
View reply reference
#12 floor 60.191.36. * And Heping [unregistered users] are good and have been resolved. Thank you.
Reply to reference
# 13f 60.191.36. * And Heping [unregistered users] are good and have been resolved. Thank you.
Reply to reference
# 14f 60.190.228. * computer_li [unregistered user]'
Reply to reference
# 15f 60.17.25. * tianke3477 [unregistered user] The installed Service can be seen in the service, but cannot be started
Tip: unable to start this service
Error 1053: the service did not respond to the start or control request in time
What is the possible cause?
Reply to reference
#218.90.165. * Judith [unregistered users] How can I not execute the code in the timer?
Reply to reference
#61.191.24 on the 17 th floor. * Peng Wanli [unregistered users:
System. componentmodel. win32exception: the account name is invalid or does not exist, or the password is invalid for the specified account name.
I use vs2005.
Reply to reference
#61.191.24 on the 18 th floor. * 2006-10-08 Peng Wanli [unregistered users] ask someone to help
Reply to reference
# Choosing loalservice on the 19 th floor [main poster] lovecherry
View reply reference
#60.208.111, building 20. * realism [unregistered user] I used C # To write a windows Service and called the method written in other DLL. The DLL was also written in C, the result is always an error when connecting to the database (but there is no problem when calling the WEB page). The database connection information is stored in the hash table as a global change, in addition, the classes for writing connection information are serialized and I am dizzy. I don't know why.
Reply to reference
#21 floor 60.176.198. * The original [unregistered users] Ask prawns:
My Timer is set to 10 seconds. When I start execution again, I want to judge whether the last program has been executed successfully. If not, I will not execute it this time. How can I write this? Help me.
Thank you.
Reply to reference
# On the 22nd floor, 60.176.198. *, I [unregistered users] asked again. I saw the service I wrote and was executed. There will be many identical processes. I think this process will exist in the next execution. If so, how can we determine this? Thank you.
Reply to reference
#24 floor 58.19.17. * tan [unregistered users]
Why is the service I wrote only executed once at startup! The interval of timer is set to 200.
Reply to reference
#25 floor 123.233.207.*2008-08-06 strangers [unregistered users] should execute the command "installutil.exe mywindowsservice.exe" to register this service ,"
How can I execute commands in this sentence? Thank you.
Reply to reference
#26 th Floor, Virus C: \ paipoint_wf \ kimbanx. uc. news \ newseexpirewindowsservice \ bin \ debug> installuti
L.exe newseexpirewindowsservice.exe
Microsoft (R). Net Framework installation utility version 1.1.4322.573
Copyright (c) Microsoft Corporation 1998-2002. All rights reserved.
An exception occurred during initialization and installation:
System. BadImageFormatException: the format of the file named newseexpirewindowsservice.exe is invalid.
..
View reply reference
# 2nd floor 220.248.17. * xxnin [unregistered users] Very strange. I made a Windows service to listen for UDP broadcast.
UDP messages cannot be received in any way.
However, I can run the code in the Form of Windows Form.
Have you encountered this problem?
Reply to reference