C # Implementation of using pallet to control Windows services

Source: Internet
Author: User
Tags exit continue tostring
window| Control Mencius E Chapter of the VB code, now from this code translated into C #, to the people accustomed to C # Reference
It is important to note that the image must be in the ICO format, otherwise it may cause the tray to not display

Using System;

Namespace Ubiserialscontroller
{
<summary>
Summary description of the CLASS1.
</summary>
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.Forms.MenuItem ("Stop", New EventHandler (StopService));
Cm. Menuitems.add (New System.Windows.Forms.MenuItem ("Pause", new EventHandler (Pauseservice));
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 void 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 ();
Switch (SC. Status)
{
Case System.ServiceProcess.ServiceControllerStatus.Stopped:
{
Ni. Icon=new System.Drawing.Icon ("Stopped.ico");
Cm. Menuitems[0]. Enabled=false;
Cm. MENUITEMS[1]. Enabled=false;
Cm. MENUITEMS[2]. Enabled=false;
Cm. MENUITEMS[3]. Enabled=true;
Break
}
Case System.ServiceProcess.ServiceControllerStatus.Running:
{
Ni. Icon=new System.Drawing.Icon ("Started.ico");
Cm. Menuitems[0]. Enabled=true;
Cm. MENUITEMS[1]. Enabled=true;
Cm. MENUITEMS[2]. Enabled=false;
Cm. MENUITEMS[3]. Enabled=false;
Break
}
Case System.ServiceProcess.ServiceControllerStatus.Paused:
{
Ni. Icon=new System.Drawing.Icon ("Paused.ico");
Cm. Menuitems[0]. Enabled=false;
Cm. MENUITEMS[1]. Enabled=false;
Cm. MENUITEMS[2]. Enabled=true;
Cm. MENUITEMS[3]. Enabled=false;
Break
}
Default
{
Ni. Icon=new System.Drawing.Icon ("Paused.ico");
Break
}
}
}

private static void Setuptimer ()
{
Timer=new System.Timers.Timer ();
Timer. interval=500;
Timer. Elapsed + = new System.Timers.ElapsedEventHandler (timer_elapsed);
Timer. Start ();
}
}
}





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.