Pick up Window Service (ii)

Source: Internet
Author: User

Pick Up Window Service (i)

ServiceController method Invocation
1PublicPartialClassService1:servicebase2{3PublicService1 ()4{5InitializeComponent ();6This. AutoLog =True;7}8ProtectedOverridevoid OnStart (String[] args)9{10//TODO: Add code here to start the service.11string state = DateTime.Now.ToString ("YYYY-MM-DD Hh:mm:ss") +"Start";12Writelog (state);Servicecontroller[] sc =Servicecontroller.getdevices ();14String str ="";15for (int i =0; I < SC. Length; i++)16{Writelog (Sc[i]. ServiceName +@"\ t"+ sc[i]. DisplayName +@"\ t" +Sc[i]. Status);18}Writelog ("");servicecontroller[] Scarr =Servicecontroller.getservices ();21stfor (int i =0; i < scarr.length; i++)22{Writelog (Scarr[i]. ServiceName +"\ t"+ scarr[i]. DisplayName +"\ t" +Scarr[i]. Status);24}ServiceController s =NewServiceController ();S.servicename ="Sina";27S.stop ();28}29ProtectedOverridevoidOnStop ()30{31//TODO: Add code here to perform the close operation required to stop the service.32string state = DateTime.Now.ToString ("YYYY-MM-DD Hh:mm:ss") +"Stop it";33Writelog (state);34}35Privatevoid Timer1_elapsed (Objectsender, System.Timers.ElapsedEventArgs e)36{Panax Notoginseng Writelog (DateTime.Now.ToString ("YYYY-MM-DD Hh:mm:ss"));38}39Publicvoid writelog (string STR) 40  {41 using (StreamWriter sw = File.appendtext (@ "c:\ Service.txt "42 Span style= "color: #000000;" > {43  SW. WriteLine (str); 44  SW. Flush (); 45 }46 }47}            

ServiceController Property

Property Describe
CanPauseAndContinue Indicates whether the service can be stopped
CanShutdown Indicates whether the service can be notified when the system shuts down, CanStop indicates whether the server can be stopped
Dependentservices Represents a device associated with a service
DisplayName Represents the name that the service controller binds to
MachineName Indicates the name of the computer on which the service resides
ServiceName Represents the service name of the binding
ServiceType Represents the type of service referenced by the controller
Servicesdependedon Indicates that the service relies on the Ude service collection status to indicate the service state referenced by the controller

ServiceInstaller class

Properties of the ServiceInstaller

Property Describe
DisplayName Display Name
ServiceName Represents the service name, which must be the same name as the Windows service that installer will install
Servicedependon An array of service names to be used by the service
StartType Represents the startup characteristics of the installed service, either automatic,manual or disable, which defaults to manual

ServiceInstaller Events

Method Event
Afterinstaller () Occurs after the install method is called
Afterrollback () Occurs after calling rollback
Afteruninstall () Occurs after calling the Uninstall method
Beforeinstall () Occurs before the install method is called
Beforerollback () Occurs before the Rollback method is called
Beforeuninstall () Occurs before the uninstall method is called
Committed () Occurs after the Commit method is called
Committing () Occurs before the Commit method is called

ServiceProcessInstaller class

The Windows service for installing ServiceBase inheritance, which is the same as the basic work done by all the services in an executable program

Property

Property Describe
Account Current user account running the service
HelpText Help information given in the Service installation options
Password Current account password to run the service
UserName User name of the current account running the service

Hang Windows Services

1, log Debugging method

2, additional process power-down debugging method

Steps:

VS Open Myserivcelog Project

Start the Myservicelog service inside the management tools-〉 Service

Click the debug-〉 of vs to attach to the process, as

In the Available Processes list, select the executable file name of the service you want to tune

Click the Attach button to enter the debug state

Set a breakpoint in the Timer1_elapsed method and wait for it to execute, and the breakpoint will be automatically started when the service executes to that point.

In addition, the timer service may periodically access the domain or non-domain pages,

Solution with WebClient

1  new WebClient ();  2  "http://127.0.0.1/rss/sina.aspx";  Byte[] by = client. Downloaddata (URI);       

Pick up Window Service (ii)

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.