问微软专家:关于Windows 服务应用程序问题

springmin 2002-06-12 05:05:26
我写了一个关于一个根据参数定时运行一程序的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
{}

}
}
}

请问:为什么会有这样?怎么解决?




...全文
97 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
acptvc 2002-06-15
  • 打赏
  • 举报
回复
To redbirdli(火鸟):
SendMessage/PostMessage 给一个窗口发消息在service中通常不work.从security的角度来看,原因很简单,普通EXE和服务是运行在不同的Windows station和desktop中的。你可以使用其他的通信机制,比如pipe等。

To springmin ():
有没有具体的错误代码什么的?你使用intallutil.exe安装你的NT service了吗?
From MSDN
"
To install your service manually
Access the directory in which your project's compiled executable file is located.
Run InstallUtil.exe from the command line with your project's output as a parameter. Enter the following code on the command line:
installutil yourproject.exe
To uninstall your service manually

Run InstallUtil.exe from the command line with your project's output as a parameter. Enter the following code on the command line:
installutil /u yourproject.exe
"

- 微软全球技术中心 VC技术支持

本贴子以“现状”提供且没有任何担保,同时也没有授予任何权利。具体事项可参见使用条款
(http://support.microsoft.com/directory/worldwide/zh-cn/community/terms_chs.asp)。
为了为您创建更好的讨论环境,请参加我们的用户满意度调查
(http://support.microsoft.com/directory/worldwide/zh-cn/community/survey.asp?key=(S,49854782))。
火鸟李国平 2002-06-12
  • 打赏
  • 举报
回复
我也有个问题问微软专家:我用VC、VB、Delphi(非.net平台)下如何实现普通EXE与服务的通讯,我用Windows的消息机制不行,如Sendmessage或PostMessage什么的,我应该怎么做呢?

110,533

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧