I recently wrote twoProgram, Can be run in two ways: service process and window process, because both types of programs require entry program, so it is very simple, as long as the entry MethodCodeTo enter the corresponding branch, the following summarizes the development experience.
In the first program, the window process and the service process have the same functions. They both query some data and send emails. The window program needs to run by adding a parameter m to the shortcut (the second program can be used without parameter judgment ). The Code is as follows:
1 Class Program
2 {
3 /**/ /// <Summary>
4///The main entry point of the application.
5/// </Summary>
6 [Stathread]
7 Static Void Main ( String [] ARGs)
8 {
9
10 // Multiple User Services can be run in the same process. To
11 // Add another service to this process. Please change the downstream
12 // Create another service object. For example,
13 //
14 // Servicestorun = new servicebase [] {New service1 (), new myseconduserservice ()};
15 //
16 If (ARGs. Length > 0 && (ARGs [ 0 ]. Toupper () = " /M " | ARGs [ 0 ]. Toupper () = " -M " ))
17 {
18 Application. enablevisualstyles ();
19 Application. setcompatibletextrenderingdefault ( False );
20 Application. Run ( New Mainform ());
21 }
22 Else
23 {
24 Servicebase [] servicestorun;
25 Servicestorun = New Servicebase [] {NewService1 ()} ;
26 Servicebase. Run (servicestorun );
27 }
28 }
29
30
When using the timer, note that the window program uses system. Windows. Forms. timer, and the service program uses system. Threading. Timer.
The second program. The window process is used to set parameters and save them in APP. config. The service process is used to run regularly. The Code is as follows: 1 Static Class Program
2 {
3 /**/ /// <Summary>
4///The main entry point of the application.
5/// </Summary>
6 Static Void Main ( String [] ARGs)
7 {
8 Bool B = False ;
9 System. serviceprocess. servicecontroller [] services;
10 Services = System. serviceprocess. servicecontroller. getservices ();
11 For ( Int I = 0 ; I < Services. length; I ++ )
12 {
13 If (Services [I]. Status = System. serviceprocess. servicecontrollerstatus. startpending &&
14 Services [I]. servicename = " Service2 " )
15 {
16B= True;
17Break;
18}
19 }
20 If (B)
21 {
22 Servicebase [] servicestorun;
23 Servicestorun = New Servicebase [] {NewService2 ()} ;
24 Servicebase. Run (servicestorun );
25 }
26 Else
27 {
28 Application. enablevisualstyles ();
29 Application. setcompatibletextrenderingdefault ( False );
30 Application. Run ( New Main ());
31 }
32 }
33 }
34
The background service process and window setting process are integrated into a program. This solution can use the same business logic layer code in the program and read the same app. config settings, you can also control the start and stop of service processes in the window process. Branch by using the portal method, and then run their own code. The code is very simple and practical.
However, there are two more questions to address.
1. The app. config setting is read only once when the process is started. The changed setting takes effect only after the process is restarted.
2. The service consumer uses installutil.exe to install and uninstall the service. You can install and uninstall the service in the Windows process.