OnStart里面要放一个运行时间很短的方法,能够成功启动该服务,当OnStart里面放一个运行时间很长的方法(比如超过2小时的方法)时就出现上面那个错误,1、请高手给个编写服务的教程或源码2、怎样才能在服务里面运行很费时间的方法,
1 回答

交互式爱情
TA贡献1712条经验 获得超3个赞
你可能需要在onStart()方法里另起一个线程,在这个线程里可以while(true).
protected override void OnStart(string[] args)
{
// TODO: Add code here to start your service.
if (threadforwork == null)
{
threadforwork = new Thread(workFunction);
}
threadforwork.IsBackground = true;
threadforwork.Start();
}
在onStop()里面将线程杀掉
protected override void OnStop()
{
// TODO: Add code here to perform any tear-down necessary to stop yourservice.
if (threadforwork != null)
{
if (threadforwork.ThreadState == System.Threading.ThreadState.Running)
{
threadforwork.Abort();
}
}
}
- 1 回答
- 0 关注
- 193 浏览
添加回答
举报
0/150
提交
取消