[转]用托盘控制windows服务的c#实现
代码
using System;
namespace UBISerialsController
{
/// <summary>
/// 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: 在此处添加构造函数逻辑
//
}
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("停止",new EventHandler(StopService)));
cm.MenuItems.Add(new System.Windows.Forms.MenuItem("暂停",new EventHandler(PauseService)));
cm.MenuItems.Add(new System.Windows.Forms.MenuItem("继续",new EventHandler(ContiuneService)));
cm.MenuItems.Add(new System.Windows.Forms.MenuItem("开始",new EventHandler(StartService)));
cm.MenuItems.Add("-");
cm.MenuItems.Add(new System.Windows.Forms.MenuItem("关于",new EventHandler(About)));
cm.MenuItems.Add(new System.Windows.Forms.MenuItem("退出",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","关于");
}
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();
}
}
}
namespace UBISerialsController
{
/// <summary>
/// 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: 在此处添加构造函数逻辑
//
}
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("停止",new EventHandler(StopService)));
cm.MenuItems.Add(new System.Windows.Forms.MenuItem("暂停",new EventHandler(PauseService)));
cm.MenuItems.Add(new System.Windows.Forms.MenuItem("继续",new EventHandler(ContiuneService)));
cm.MenuItems.Add(new System.Windows.Forms.MenuItem("开始",new EventHandler(StartService)));
cm.MenuItems.Add("-");
cm.MenuItems.Add(new System.Windows.Forms.MenuItem("关于",new EventHandler(About)));
cm.MenuItems.Add(new System.Windows.Forms.MenuItem("退出",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","关于");
}
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();
}
}
}
但是有人评论说“这种方式决不是MS推荐的方式。本应该来自异步状态通知的钩子架构改成了定时器...写了十多年程序,最不齿的就是定时器,从来不用(javascript例外)。并不是仅仅因为定时器额外消耗资源,而且定时器逻辑隐藏着某种不合理性。 ”