.NET auto service - C # (2)
Visualsw
Start service:
Define two variables:
Private string [,] arrconf; save configuration information
Private assembly [] assobj; loading components
Add the following code in the onStart event:
protected override void onstart (String [] ARGS)
{
Writelog ("/ *************************************************** ************** / ");
Writelog ("ScheculerServer Start AT" DateTime.now.toString ());
// load configuation
IF (! ReadConf ()) return;
// load assembly
Try
{
Assobj = new assembly [arrconf.getlength (1)];
For (int i = 0; i { Assobj [I] = askMBLY.LOADFROM (ArrConf [0, i] .tostring ()); Arrconf [0, i] = "notruning"; } } Catch (Exception E) { WriteLog (datetime.now.toString ()); WriteLog ("Load DLL Error:"); WriteLog (e.tostring ()); } // Start Time Schedulertimer.Interval = 30000; // Setting every 30 seconds Schedulertimer.enabled = true; // Start Timer } Timer triggers: Do check whether the check time is run here Private void schedulertimer_Elapsed (Object sender, System.Timers.ELAPSEDEventArgs E) { Try { //SCHEDULERTIMER.ENABLED=FALSE DateTime DTNOW = DATETIME.NOW; Datetime DTRUN; For (int i = 0; I { Dtrun = convert.todatetime (Arrconf [1, i] .tostring ()); IF (DTRUN.Addseconds (-30) <= DTNOW && DTNOW <= DTRUN.AddSeconds (30)) { IF (ArrConf [0, I] .tostring (). Trim () == "notruning") { Foreach (Type T in Assobj [i] .gettypes ()) { IF (T.Isclass &&! T.isabstract && T.ISPUBLIC) { Object obj = activator.createInstance (T); MethodInfo mi = T. GetMethod ("run"); IF (mi! = null) { mi.invoke (Obj, null); Obj = NULL; Gc.collect (); Break; } Obj = NULL; Gc.collect (); } } Arrconf [0, I] = "Onruning"; WriteLog ("---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- ------------------------- "); Writelog (DateTime.now.toString () ": Runing" Arrconf [1, i] " assobj [i] .location.toString ()); WriteLog ("--------------------------------------------- -------------- "); } } Catch (Exception EX) { WriteLog ("##################################################################### ########### "); Writelog (ex.toswoting ()); WriteLog ("##################################################################### ########### "); } } At this time, the main skeleton of the program is complete, the next step needs to make a Windows service installer, switch to the design page, with the upper right feet of the property, the Add Installer word, click, VS.NET to help you generate the installer. Open the ProjectInstaller.cs file, you can see two components and set their properties. Run the account Select Localsystem, and the system runs in the local system account. Start type selection automatic. Compile to EXE, open VS.NET CMD, enter the exe directory, run installutil.exe schedulerser.exe installation service, after success, open service management to see the service, start the service. Service log Save SchedulerServer.txt in the system directory / ************************************************** *********** / ScheculerServer Start AT 12/24/2003 3:46:21 PM -------------------------------------------------- ----------- 12/24/2003 3:46:51 PM: RUNING 15:47 E: /Work/geid/src/geidautocheck/bin/debug/geidautocheck.dll -------------------------------------------------- ----------- ScheculerServer Stop At 12/24/2003 3:48:55 PM / ************************************************** *********** / Automatically run the program: 1. .NET components (DLL) 2. Entry Public void Run () {}