问微软专家:关于Windows 服务应用程序问题
我写了一个关于一个根据参数定时运行一程序的Windows 服务应用程序,调试时能正确运行,但发布在服务器上后,不能运行(不能去运行被调用的程序)
程序如下:
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.ServiceProcess;
namespace EmailServer
{
public class EmailServer : System.ServiceProcess.ServiceBase
{
private System.Timers.Timer timerControl;
public string Program;
public System.Diagnostics.Process prc;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;
public EmailServer()
{
// 该调用是 Windows.Forms 组件设计器所必需的。
InitializeComponent();
// TODO: 在 InitComponent 调用后添加任何初始化
}
// 进程的主入口点
static void Main()
{
System.ServiceProcess.ServiceBase[] ServicesToRun;
// 同一进程中可以运行多个用户服务。若要将
// 另一个服务添加到此进程,请更改下一行
// 以创建另一个服务对象。例如,
//
// ServicesToRun = New System.ServiceProcess.ServiceBase[] {new Service1(), new MySecondUserService()};
//
ServicesToRun = new System.ServiceProcess.ServiceBase[] { new EmailServer() };
System.ServiceProcess.ServiceBase.Run(ServicesToRun);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器
/// 修改此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.timerControl = new System.Timers.Timer();
((System.ComponentModel.ISupportInitialize)(this.timerControl)).BeginInit();
//
// timerControl
//
this.timerControl.Enabled = true;
this.timerControl.Interval = 1000;
this.timerControl.Elapsed += new System.Timers.ElapsedEventHandler(this.timerControl_Elapsed);
//
// EmailServer
//
this.CanHandlePowerEvent = true;
this.CanPauseAndContinue = true;
this.CanShutdown = true;
this.ServiceName = "EmailServer";
((System.ComponentModel.ISupportInitialize)(this.timerControl)).EndInit();
}
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
/// <summary>
/// 设置具体的操作,以便服务可以执行它的工作。
/// </summary>
protected override void OnStart(string[] args)
{
Console.WriteLine("start");
if(args!=null && args.Length>1)
{
try
{
this.timerControl.Interval=Convert.ToInt32(args[0])*1000;
this.Program=args[1];
}
catch
{
this.timerControl.Interval=10000;//10s
}
}
else
this.timerControl.Interval=10000;//10s
timerControl.Start();
}
/// <summary>
/// 停止此服务。
/// </summary>
protected override void OnStop()
{
timerControl.Stop();
}
private void timerControl_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
// RecceiptEmail();
// try
// {
// prc.Kill();
// }
// catch
// {
// }
try
{
prc=System.Diagnostics.Process.Start(this.Program);
}
catch
{}
}
}
}
请问:为什么会有这样?怎么解决?