Click here to Skip to main content
15,892,746 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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:


C#
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);
        }
    }
}
Posted
Updated 13-Nov-11 14:36pm
v2

1 solution

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
 
Share this answer
 
v2
Comments
thatraja 14-Nov-11 0:47am    
Big 5!
Sergey Alexandrovich Kryukov 14-Nov-11 0:49am    
Big thank you!
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900