Control Windows service C # implementation with tray

xiaoxiao2021-03-05  31

Using

System;

Namespace

UbiserialsController

...

{/ ** ////

/// Class1 Summary Description.

/// public class Class1 ... {static System.ServiceProcess.ServiceController sc; static System.Windows.Forms.NotifyIcon ni; static System.Windows.Forms.ContextMenu cm; static System.Timers.Timer timer; Public class1 () ... {// // Todo Add Constructor Logic here //} static void main () ... {Try ... {sc = new system.serviceProcess.ServiceController ("ubiserials" ); Ni = new system.windows.Forms.notifyicon (); ni.visible = false; cm = new system.windows.forms.contextMenu (); cm.menuitems.add (new system.windows.form.Menuitem (" Stop ", New EventHandler (StopService))))); cm.menuitems.add (New System.Windows.Forms.Menuitem)); cm.menuitems.add (New System.Windows.Forms . MeNuitem ("Continue", New EventHandler (ContiUneService))))))))))))))))))))))); cm.menuitems.add (New System.Windows.Forms.Menuitem ("Start", New EventHandler (StartService))); cm.MenuItems.Add ("- "); Cm.menuitems.add (New System.windows.Forms.Menuitem (" About ", New EventHandler (About))); cm.MenuItems.Add ( New System.Windows.Forms.Menuitem ("Exit", New EventHandler (Exit))); ni.contextMenu = cm; ni.visible = true; setuptimer (); system.windows.Forms.Application.run (); Catch (System.Exception EX) ... {system.windows.forms.MessageBox.show (ex.Message.Tostring ());}} private static void stopservice (Object Sender, System.EventArgs E) ... {IF (sc.status == system.serviceProcess.serviceControllerstatus.running && sc.canstop == true) ... {try ... {sc.stop ();} catch (system.exception ex) ... {system.windows. Forms.MessageBox.show (ex.Message.toString ());

}}} Private static void PauseService (object sender, System.EventArgs e) ... {if (sc.Status! = System.ServiceProcess.ServiceControllerStatus.Paused && sc.CanPauseAndContinue == true) ... {try ... {sc .Pause ();} catch (system.Exception ex) ... {system.windows.forms.MessageBox.show (ex.Message.toString ());}}} private static void ContiUneService (Object Sender, System.EventArgs e) ... {IF (sc.status == system.serviceProcess.serviceControllerstatus.paused && sc.canpauseandcontinue == true) ... {try ... {sc.continue ();} catch (system.exception ex). .. {system.windows.forms.MessageBox.show (ex.Message.toString ());}}} Private static void startservice (object sender, system.eventargs e) ... {if (sc.status == system .ServiceProcess.ServiceControllerstatus.stopped ... {try ... {sc.start ();} catch (system.exception ex) ... {system.windows.Forms.MessageBox.show (ex.Message.Tostring ));}}} Private static vo ID About (Object Sender, System.Eventargs E) ... {System.windows.Forms.MessageBox.show ("2005.4.3", "About");} Private Static Void Exit (Object Sender, System.EventArgs E) ... {Try ... {Timer.dispose (); sc.dispose (); cm.dispose (); ni.dispose (); system.windows.forms.Application.exit ();} catch (system. Exception ex) ... {System.Windows.Forms.MessageBox.Show (ex.Message.ToString ());}} private static void timer_Elapsed (object sender, System.Timers.ElapsedEventArgs e) ... {GetServiceState () } Private static void getServiceState () ... {sc.refresh ();

转载请注明原文地址:https://www.9cbs.com/read-33632.html

New Post(0)