Windows Service examples (System Event Logs, Run batch processing, install/uninstall, start debugging)

Source: Internet
Author: User

Generally, Windows Services are mostly used for monitoring, and the application scope includes hardware control and applicationProgramMonitoring, system-level applications, diagnostics, reporting, web and file system services are widely used.

This section briefly describes the basic use of Windows Services, that isCodeCompile, debug, install, and uninstall the SDK.

After creating a Windows Service Project, you can see that the start and stop methods are reloaded in the service. Of course, there are pause and continue methods ......

Protected   Override   Void Onstart ( String [] ARGs)
{
Thread. Sleep ( 1000   *   10 );
Mainthread. Start ();
}

Protected Override VoidOnstop ()
{
Mainthread. Abort ();
}

By the way, Service Code generally uses operations such as writing files, threads, and reading and writing user information through the registry, so these applications are also very reusable.

Streamwriter SW =   Null ;
If ( ! File. exists (filepath ))
{
SW = File. createtext (filepath );
}
Else
{
SW = File. appendtext (filepath );
}

If(SW! = Null)
{
Sw. writeline ("------------------------- Monitoring log ---------------------");
Sw. writeline (currentdatetime. tostring ());
Sw. writeline (applicationlog );
Sw. Close ();
}

Windows Log

String [] Logs =   New   String [] { " Application " /* , "System" */ };
Stringbuilder result =   New Stringbuilder ();
Foreach ( String Log In Logs)
{
EventLog =   New EventLog ();
EventLog. Log = Log;
// EventLog. Source = "application error ";
Foreach (Eventlogentry entry In EventLog. Entries)
{
If (Entry. timegenerated > = Querytime | Entry. timewritten > = Querytime)
{
If (Entry. entrytype = Eventlogentrytype. Error && Entry. Source =   " Application error " )
{
Result. append (log );
Result. append (entry. entrytype. tostring ());
Result. append ( " ( "   + Entry. timewritten. tostring () +   " ): " );
Result. append (entry. Message );

Isrestartampjettyservice= True;
}
}
}
}

Operation regedit, execute BAT file, kill Process

String Keypath =   " Software \ Microsoft \ Windows \ CurrentVersion \ Explorer \ Shell Folders " ;
Registrykey currentuser = Registry. currentuser;
Registrykey shellkey = Currentuser. opensubkey (keypath );

If (Softwarekeys ! =   Null )
{
Desktopvalue = Shellkey. getvalue ( " Desktop " ). Tostring ();
Result =   True ;
} Process =   New Process ();
Process. startinfo. filename = Desktoppath +   " Serverjetty. bat " ;
Process. startinfo. useshellexecute =   False ;
Process. startinfo. redirectstandardoutput =   True ;
Process. startinfo. createnowindow =   True ;
Process. Start ();

Writelog (process. standardoutput. readtoend ()); Process [] Processes = Process. getprocesses ();
Foreach (Process In Processes)
{
If (Process. processname. tolower (). compareto ( " Java " ) =   0 )
{
Process. Kill ();
}
}

Write and install and uninstall services

Go to the service design page, right-click and select "add installer" to add the corresponding service, as shown below:

Set names, descriptions, application accounts (generally LocalSystem, LocalService, networkservice, and user) and startup types (automatic, manual, and disabled) for serviceinstaller and serviceprocessinstaller respectively ).

After completing the above operations, you can basically install this service. Now, you only need to install this service. The project cannot run or debug directly through F5. Attach process is required for debugging, install the service before debugging.

Install the command for the uninstall service, and use installutil.exe in .netframework. (WIN Vista/7/note that when you run this command, set compatibility to "run as administrator ")

Installutil.exe is in the c: \ windows \ Microsoft. NET \ framework \ v4.0.30319 directory. The directory is determined based on the version number,

Service Installation result,

Service debugging, directly F5

This is not the case. You need to attach it to a process. To attach it to a short range, you need to start the service to load the process. Otherwise, you cannot find the process.

, Directly (CTRL + ALT-p) the same

In this way, you can debug the code, and then you can do all kinds of coding work!

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.