Window service used in recent projects Program I have never touched on it before. I am a stranger. I spent two days studying it and wrote a simple service. However, when I made the installation program, I referred to a lot of information on the Internet, however, they cannot be created successfully. It may be because the development environment or project configuration is different. Here, we will post the method we have created successfully. On the one hand, we will leave it to ourselves, on the one hand, we hope that we can be able to find ourselves in despair and lose the soul of passers-...
IDE: win2003 vs2008 sql2005
1. Create a window Service Project
First, create a window Service Project: solution (right-click) --> Add --> Create a project --> select windows as the project type --> select windows as the template,
2. Compile the window service program
After creation, two files, program. CS and service1.cs, are generated automatically. We add the required operations to service1.cs. There are two rewrite Methods onstart and onstop, onstart indicates the operation to start the service, and onstop indicates the operation to stop the service. Below is a simple example: after the service is started, call the hello function of a remote WebService and write the returned value to a file. The Code is as follows:
Code
/// <Summary>
/// Service Startup operations
/// </Summary>
/// <Param name = "ARGs"> </param>
Protected Override Void Onstart ( String [] ARGs)
{
Starthello ();
}
/// <Summary>
/// Service stop operation
/// </Summary>
Protected Override Void Onstop ()
{
Try
{
Threadhello. Abort ();
Flag= False;
system. diagnostics. trace. write ( " stop a thread " );
}< br> catch (exception ex)
{< br> system. diagnostics. trace. write (ex. message);
}< BR >}
PrivateThread threadhello;
Private Void Starthello ()
{
Try
{
// Standard Format
// Threadstart newthreadstart = new threadstart (voidname );
// Thread newthead = new thread (newthreadstart );
// Newthead. Start ();
Threadhello = New Thread ( New Threadstart (Hello ));
Threadhello. Start ();
System. Diagnostics. Trace. Write ( " Thread task start " );
}
Catch (Exception ex)
{
System. Diagnostics. Trace. Write (ex. Message );
Throw Ex;
}
}
Private Void Hello ()
{
While (FLAG)
{
Localhost. advservice la = New Windowsservice1.localhost. advservice ();
Try
{
String Helloname = La. Hello ( " Andy " ); // Call methods in remote WebService
Writeinlog (helloname, False ); // Write the data returned by the call to the file
}
Catch (Exception ex)
{
System. Diagnostics. Trace. Write (ex. Message );
Throw Ex;
}
Thread. Sleep (5000);
}
}
/// <Summary>
/// File write operations
/// </Summary>
/// <Param name = "MSG"> Write content </Param>
/// <Param name = "isautodelete"> Delete? </Param>
Private Void Writeinlog ( String MSG, Bool Isautodelete)
{
Try
{
String Logfilename = @" E: \ sample \ pg_sample \ log.txt " ; // File Path
Fileinfo = New Fileinfo (logfilename );
If (Isautodelete)
{
If (Fileinfo. exists && Fileinfo. Length > = 1024 )
{
Fileinfo. Delete ();
}
}
Using (Filestream FS = Fileinfo. openwrite ())
{
Streamwriter SW = New Streamwriter (FS );
Sw. basestream. Seek ( 0 , Seekorigin. End );
Sw. writeline ( " ============================================ " );
Sw. Write ( " Date added: " + Datetime. Now. tostring () + " \ R \ n " );
Sw. Write ( " The log Content is: " + MSG + " \ R \ n " );
Sw. writeline ( " ============================================ " );
Sw. Flush ();
Sw. Close ();
}
}
Catch (Exception ex)
{
Ex. tostring ();
}
}
The example is simple and easy to understand. The preceding operations are executed every five seconds through a thread. Here, the functions of our services and services have been written, how can I install our services on the client? I wrote this article here.ArticlePurpose! First, add an installer class, switch to the View window, right-click --> add installer, and a program installation class file projectinstaller. CS will be added,
Here, serviceprocessinstaller1 and serviceinstaller1 have several important attributes to be set. starttype in serviceinstaller1 must be set to automatic, indicating random start, servicename indicates service name, description indicates service description, and displayname indicates display name. Set the account in serviceprocessinstaller1 to LocalSystem, indicating the local system account.
There are two ways to install the window service:
I. Command Installation
In the command window, you can run the installutil.exe service path to install the window service. Do you think I'm pulling this long command? How to remember? You can think that if the client is not a programmer, it is impossible to install it on its own. Can you make a simple and effective installation by clicking the button, the answer is yes --> Installation Program Creation 2. Installation Program Creation: solution (right-click) --> Add --> Create a project --> select install and deploy for the project type --> select install project for the template: after the project is created, install the project (right-click) --> View --> File System
Because we are installing the service, you do not need to use the desktop and program menu. You can directly use the application folder (right-click) --> Add --> project output graph:
There are two points to note: select the service to be installed from the first project selection, select the main output from the second project selection, and click OK. In this step, the installer folder is added, the next step is to add operations for this installer. Here we add two basic operations: Install and uninstall. Installation Project (right-click) --> View --> Custom operation
We can see the installation, submission, rollback, uninstallation, and other operations above. We will first add the installation operation and install it (right-click) --> Add a custom operation. A dialog will pop up.
Select the application folder, select the previously added primary output item, and click OK. In this case, the installation operation is added for this installer, And the uninstallation operation is also added in this way, the only difference between uninstallation and installation is that you need to set a command parameter,
Here, input/u in arguments to indicate that the uninstall command is equivalent to the installutil.exe/u service path. Here, our installation program has been created, and the installation program project will be generated. setup.exe and Setup will be generated. if you click setup.exe to install our service like QQ, it is actually much better than QQ. QQ has advertisements. We are pure green!