欢迎光临
我们一直在努力

用托盘控制windows服务的c#实现-.NET教程,C#语言

建站超值云服务器,限时71元/月

孟子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();

}

}

}

赞(0)
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » 用托盘控制windows服务的c#实现-.NET教程,C#语言
分享到: 更多 (0)