1

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);
            }
        }
    }
  • 5
    Windows kills your service so that shutting down the computer doesn't take too long. Don't try to circumvent that; it will be extremely annoying. – SLaks Nov 14 '11 at 02:06
  • @SLaks: I think it is the whole point of his program. Considering that, I think the "annoyance" would be deliberate. He couldn't really share the program without stating what it does - block shutdown to backup data. The mechanism isn't the best one for the purpose, though, as if he ever wants to kill the service it will refuse/pause while it backs up data... – Merlyn Morgan-Graham Nov 14 '11 at 02:13
  • 5
    Be aware that Microsoft will continue to work to prevent programs like yours from existing. Your basic premise is wrong. Backing up data at system shutdown is the wrong time - the system is shutting down. Pretend the reason for the shutdown is there is only 2 seconds of battery life left. Now go redesign your entire system. (in other words, you need to backup periodically; not at shutdown) – Ian Boyd Nov 14 '11 at 12:27

1 Answers1

1

Apart from your query, I want to remind you that the services are running in a separate logon session and the services won't interact with the logged in desktop session (mostly).

So, you need to intercept the shutdown event in your service code. Then, you need to hold the shutdown event till you complete your backup process. You can hook those Windows events through message pumps/queues. You need to intercept the WM_ENDSESSION/ WM_QUERYENDSESSION events.

This query is already discussed in this post. You can refer that.

Community
  • 1
  • 1
Aravind
  • 11
  • 1