Windows Services-操作系统问题 [英] Windows Services - problem with operating system

查看:91
本文介绍了Windows Services-操作系统问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,我想对我的英语致歉.我尝试创建Windows服务,以便在计算机关闭时为BuckUp数据运行程序.
问题是操作系统在关机期间要杀死我的Windows服务,然后才执行备份数据,直到结束.我将注册表值HKEY_LOCAL_MACHINE \ SYSTEM \ CurrentControlSet \ Control \ WaitToKillServiceTimeout更改为3600000,但这没有帮助,我的Windows Service在执行前被杀死.也许有人知道如何使操作系统不会杀死Windows服务,因为可以快速备份数据.请帮助我,我正在等待您的回复.下面,我包括我的代码Windows服务:


Firstly I would like to apologize for my English language. I try to create a Windows Service which run program for BuckUp data when the computer is shutting down.
Problem is that the operating system during shutdown to kill my Windows Service before BackUp data is executed by to the end of. I changed the registry value HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\WaitToKillServiceTimeout to 3600000 but it didn''t help, my Windows Service is killed before it is executed. Maybe someone knows how to make the operating system does''t kill the Windows Service as quickly to BackUp data could be made. Please help me, I''m waiting for your response. Below I include my code Windows Service:


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.IO;

namespace backUp_ser
{
    public partial class Service1 : ServiceBase
    {
        public Service1()
        {
            InitializeComponent();
            this.CanShutdown = true;
        }

        protected override void OnStart(string[] args)
        {
        }

        protected override void OnStop()
        {
        }

        protected override void OnShutdown()
        {
            ProcessStartInfo stratInfo = new ProcessStartInfo();
            stratInfo.WindowStyle = ProcessWindowStyle.Hidden;
            stratInfo.FileName = "C:\\Program Files\\Cobian Backup 10\\Cobian.exe";
            stratInfo.Arguments = "list:C:\\Program Files\\Cobian Backup 10\\DB\\MainList.lst -bu -nogui -autoclose";

            Process process = Process.Start(stratInfo);
            process.WaitForExit(360000);
        }
    }
}

推荐答案

您的主要问题是您启动了另一个进程Cobian.exe.这没有任何意义-没有什么可以阻止OS终止该进程.使用整数参数调用WaitForExit也是没有意义的,尤其是使用此立即常量时.您只需为等待创建超时;换句话说,如果该过程尚未终止,您可能会停止等待.另外,在任何情况下都没有必要使用硬编码的文件路径名.您可以使用一些配置文件找到正确的路径参数.

也许关闭计算机不是执行备份的好时机.有些人不会关闭很多天,也不会使用待机或休眠模式.关闭计算机并不是备份更有价值的特殊时刻:任何时候都可能发生故障,与关闭或启动无关.

通常,备份是按照一定的时间表进行的. 您不需要编写自己的Windows服务.您需要的服务已经存在,称为 Windows Task Scheduler ,请参见:
http://en.wikipedia.org/wiki/Task_Scheduler [ http://msdn.microsoft.com/en-us/library/aa383614.aspx [ ^ ],
http://msdn.microsoft.com/en-us/library/aa384006%28v = VS.85%29.aspx [ ^ ].

有关使用Windows Task Scheduler的信息,请参见上面的最后一个链接.您可以使用 Task Scheduler Managed Wrapper 在程序中使用其API,请参见 http://taskscheduler.codeplex.com/ [^ ].

但是,如果它甚至更简单.
您可以通过Windows实用程序AT.EXE或SchTasks.EXE使用Windows Task Scheduler,请参见:
http://en.wikipedia.org/wiki/At_(Windows) [ http://technet.microsoft.com/en-us/library/bb490866.aspx [ ^ ],
http://en.wikipedia.org/wiki/Schtasks [ http://msdn.microsoft.com/zh-我们/library/windows/desktop/bb736357%28v=vs.85%29.aspx [
Your main problem is that you start another process Cobian.exe. It makes no sense — nothing prevents OS to kill this process. The call to WaitForExit using integer parameters also makes no sense, especially using this immediate constant. You simply create a timeout for the wait; in other words, if the process is not yet terminated you might stop waiting. Also, there is no situations when a hard-coded file path name can be useful. You could find out the right path parameters using some configuration file.

Maybe the shutdown of the computer is not a good time to perform the backup. Some people never shutdown for many days, or use stand-by or hibernate. Shutdown of a computer is not a special moment where the backup is more valuable anyway: a trouble can happen anytime, at the moment of time not related to shutdown or start up.

Usually, backup is done according to some schedule. And you don''t need to write your own Windows Service. The Service you need already exists, called Windows Task Scheduler, see:
http://en.wikipedia.org/wiki/Task_Scheduler[^],
http://msdn.microsoft.com/en-us/library/aa383614.aspx[^],
http://msdn.microsoft.com/en-us/library/aa384006%28v=VS.85%29.aspx[^].

See the last link above for use of the Windows Task Scheduler. You can use its API in your program using Task Scheduler Managed Wrapper, see http://taskscheduler.codeplex.com/[^].

But if can be even simpler.
You can use Windows Task Scheduler using Windows utilities AT.EXE or SchTasks.EXE, see:
http://en.wikipedia.org/wiki/At_(Windows)[^],
http://technet.microsoft.com/en-us/library/bb490866.aspx[^],
http://en.wikipedia.org/wiki/Schtasks[^],
http://msdn.microsoft.com/en-us/library/windows/desktop/bb736357%28v=vs.85%29.aspx[^].

—SA


这篇关于Windows Services-操作系统问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆