Windows service installation (C #)

Source: Internet
Author: User

1. Create a Service Project

For example

Using system;
Using system. collections;
Using system. componentmodel;
Using system. Data;
Using system. diagnostics;
Using system. serviceprocess;
Using system. Threading;
Using system. Windows. forms;
Namespace catcharticle
{
Public class catcharticle: system. serviceprocess. servicebase
{
/// <Summary>
/// Required designer variables.
/// </Summary>
Private system. componentmodel. Container components = NULL;
Private thread mainthread;
Public catcharticle ()
{
// This call is required by the windows. Forms component designer.
Initializecomponent ();

Mainthread = new thread (New threadstart (threadfunc ));
Mainthread. Priority = threadpriority. Lowest;
}

// Master entry point of the process
Static void main ()
{
System. serviceprocess. servicebase [] servicestorun;
Servicestorun = new system. serviceprocess. servicebase [] {New catcharticle ()};
System. serviceprocess. servicebase. Run (servicestorun );
}

/// <Summary>
/// The designer supports the required methods-do not use the code editor
/// Modify the content of this method.
/// </Summary>
Private void initializecomponent ()
{
Components = new system. componentmodel. Container ();
This. servicename = "catcharticle ";
}

/// <Summary>
/// Clear all resources in use.
/// </Summary>
Protected override void dispose (bool disposing)
{
If (disposing)
{
If (components! = NULL)
{
Components. Dispose ();
}
}
Base. Dispose (disposing );
}

/// <Summary>
/// Set specific operations so that the service can perform its work.
/// </Summary>
Protected override void onstart (string [] ARGs)
{
Mainthread. Start ();
}
 
/// <Summary>
/// Stop the service.
/// </Summary>
Protected override void onstop ()
{
Mainthread. Abort ();
}

Public static void threadfunc ()
{
Int lasthour = datetime. Now. hour;
While (true)
{
System. Threading. thread. Sleep (1000 );
If (datetime. Now. Hour-1 = lasthour)
{
MessageBox. Show ("to take good care of your eyes, please take a 5-minute break and look into the distance! "," Warning ", messageboxbuttons. OK, messageboxicon. Warning, messageboxdefaultbutton. button1, messageboxoptions. defaulttoptoponly );
Lasthour = datetime. Now. hour;
}
}
}

}
}

 

==========================================================

2. Add a project install. CS inherited from the installer. Some parameters can be defined by yourself.

Using system. collections;
Using system. configuration. Install;
Using system. serviceprocess;
Using system. componentmodel;

Namespace catcharticle
{
/// <Summary>
/// Summary of myinstall.
/// </Summary>
///
[Runinstaller (true)]
Public class myinstall: Installer
{

Private serviceinstaller;
Private serviceprocessinstaller processinstaller;
Public myinstall ()
{
Processinstaller = new serviceprocessinstaller ();
Serviceinstaller = new serviceinstaller ();

Processinstaller. Account = serviceaccount. LocalSystem;
Serviceinstaller. starttype = servicestartmode. Automatic;
Serviceinstaller. servicename = "catcharticle ";

Installers. Add (serviceinstaller );
Installers. Add (processinstaller );
}
}
}

 

 

3. Use release

4. Use c: \... \ framework \ v1.1.. \ installutil.exe c: \ Project path \ bin \ release \ testservice.exe

 

NOTE: If projectinstall. CS is not added, the following error is reported:

Installing the assembly "F: \ website directory \ edzh.com \ windowsservice \ bin \ debug \ windowsservice.exe ".
The affected parameters are:
Assemblypath = F: \ website directory \ edzh.com \ windowsservice \ bin \ debug \ windowsservice.exe
Logfile = F: \ website directory \ edzh.com \ windowsservice \ bin \ debug \ windowsservice. installlog
There is no public installer for runinstallerattribute. The "yes" attribute may be found in the F: \ website directory \ edzh.com \ windowsservice \ bin \ debug \ windowsservice.exe program.
Submitting the assembly "F: \ website directory \ edzh.com \ windowsservice \ bin \ debug \ windowsservice.exe ".
The affected parameters are:
Assemblypath = F: \ website directory \ edzh.com \ windowsservice \ bin \ debug \ windowsservice.exe
Logfile = F: \ website directory \ edzh.com \ windowsservice \ bin \ debug \ windowsservice. installlog
There is no public installer for runinstallerattribute. The "yes" attribute may be found in the F: \ website directory \ edzh.com \ windowsservice \ bin \ debug \ windowsservice.exe program.
No installer, so the installstate file is removed.
Installing the assembly "F: \ website directory \ edzh.com \ windowsservice \ bin \ debug \ windowsservice.exe ".
The affected parameters are:
Assemblypath = F: \ website directory \ edzh.com \ windowsservice \ bin \ debug \ windowsservice.exe
Logfile = F: \ website directory \ edzh.com \ windowsservice \ bin \ debug \ windowsservice. installlog
There is no public installer for runinstallerattribute. The "yes" attribute may be found in the F: \ website directory \ edzh.com \ windowsservice \ bin \ debug \ windowsservice.exe program.
Submitting the assembly "F: \ website directory \ edzh.com \ windowsservice \ bin \ debug \ windowsservice.exe ".
The affected parameters are:
Assemblypath = F: \ website directory \ edzh.com \ windowsservice \ bin \ debug \ windowsservice.exe
Logfile = F: \ website directory \ edzh.com \ windowsservice \ bin \ debug \ windowsservice. installlog
There is no public installer for runinstallerattribute. The "yes" attribute may be found in the F: \ website directory \ edzh.com \ windowsservice \ bin \ debug \ windowsservice.exe program.
No installer, so the installstate file is removed.

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.