[急]求助:创建windows服务

hyc1218 2009-03-12 02:25:34
创建windows服务并注册后,启动服务时系统总是提示‘服务启动后又停止了’。我是第一次写服务,请高手帮忙解答。以下是服务源码:
我是想通过服务调用一个winfrom的程序
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.ServiceProcess;
using System.IO;

namespace WsBrowsePages
{
public partial class BOpenService2 : ServiceBase
{
bool servicePaused;
private Process proc;
public BOpenService2()
{
InitializeComponent();
}

protected override void OnStart(string[] args)
{
proc = new Process();
proc.StartInfo.FileName = "D:\\BrowsePages\\BrowsePages\\bin\\Debug\\BrowsePages.exe";
proc.StartInfo.Password = null;
proc.StartInfo.UserName = null;
proc.StartInfo.CreateNoWindow = true;
proc.Start();
FileSystemWatcher curWatcher = new FileSystemWatcher();
}

protected override void OnStop()
{
//// TODO: 在此处添加代码以执行停止服务所需的关闭操作。
proc.Close();
proc.Dispose();
}

protected override void OnPause()
{
servicePaused = true;
}
protected override void OnContinue()
{
servicePaused = false;
}
}
}

using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;

namespace WsBrowsePages
{
[RunInstaller(true)]
public class Installer1 : System.Configuration.Install.Installer
{
///
/// 必需的设计器变量.
///
private System.ComponentModel.Container components = null;
private System.ServiceProcess.ServiceProcessInstaller spInstaller;
private System.ServiceProcess.ServiceInstaller sInstaller;
public Installer1()
{
// 该调用是设计器所必需的.
InitializeComponent();
// TODO: 在 InitComponent 调用后添加任何初始化
}
#region Component Designer generated code

///
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容.
///
private void InitializeComponent()
{
components = new System.ComponentModel.Container();
// 创建ServiceProcessInstaller对象和ServiceInstaller对象

this.spInstaller =
new System.ServiceProcess.ServiceProcessInstaller();
this.sInstaller = new System.ServiceProcess.ServiceInstaller();
// 设定ServiceProcessInstaller对象的帐号、用户名和密码等信息
this.spInstaller.Account =

System.ServiceProcess.ServiceAccount.LocalSystem;
this.spInstaller.Username = null;
this.spInstaller.Password = null;
// 设定服务名称
this.sInstaller.ServiceName = "BOpenService2";
// 设定服务的启动方式
this.sInstaller.StartType = System.ServiceProcess.ServiceStartMode.Automatic;
this.Installers.AddRange(
new System.Configuration.Install.Installer[] { this.spInstaller,this.sInstaller });
}

#endregion
}

}
...全文
58 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
hyc1218 2009-03-12
  • 打赏
  • 举报
回复
的确是因为没有一个一直运行的进程导致的,非常感谢
hxxxs 2009-03-12
  • 打赏
  • 举报
回复
检查一下配置呢,会不会是ip地址什么的没设置对?
hyc1218 2009-03-12
  • 打赏
  • 举报
回复
程序我在本机运行过,没有报错的。
  • 打赏
  • 举报
回复
停止的原因是因为你的程序报错导致的,你不妨把你的代码用trycacth包括起来,然后把错误输出到日志,看是什么错误,这样才能确定原因,不然你发帖子也不见得有人知道

用windows service的一个不好的地方就是不好调试,但可接触日志来看
hyc1218 2009-03-12
  • 打赏
  • 举报
回复
哦,是要定义一个始终运行的线程是调用它吗?
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 jinjazz 的回复:]
起码要保持一个线程在运行,你这样当然要退出了
[/Quote]

windows service需要保证一个线程在运行吗?好像不要吧?那且不是做一个windows service 一定要浪费一个线程的资源?这说不过去。
jinjazz 2009-03-12
  • 打赏
  • 举报
回复
起码要保持一个线程在运行,你这样当然要退出了

111,126

社区成员

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

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

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