首页 > 开发 > 综合 > 正文

用托盘控制windows服务的c#实现

2024-07-21 02:18:11
字体:
来源:转载
供稿:网友


收集最实用的网页特效代码!

孟子e章给出的vb代码,现在从这个代码翻译成c#,给习惯c#的人参考
需要注意的是图片一定需要是ico格式,否则可能会导致托盘的不显示

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();
}
}
}



发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表