0
votes

I made a service (WinServiceProject) many days ago and I want that this start automatic but with a Delayed Time... I found a solution but this cant help me: http://support.microsoft.com/kb/193888/en-us

I modify the regedit at HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\WinServiceProject but this doesn't work.

I add Multi-String Value (DependOnService) and set many services before... this works but dont like I want.

The solution I need its with time, set many time and execute the service after this time. If I need add code to my service I will do it.

Thanks.

1
Although not ideal, you could consider inserting a delay into your service code to prevent it operating for n seconds. It would need to be on the main service thread and not the thread that calls OnStart() otherwise it may appear that the service doesn't start successfully.Martin
Martin Parkin - Yes I think that maybe the solution is add a code to the service where I stop the service and set "x" time to start again... making a restart function.Manuel Fdz
@ManuelFdz, based on your reply, I am wondering if you need the service to continue to do something every "x" minutes. Is that correct?AWinkle
@AWinkle, No, my service only run one time and send some data to SQL... but I need wait many time to start.Manuel Fdz
Thread.Sleep([your time in ms]);AWinkle

1 Answers

0
votes

I resolve the problem with this code!!

public static void RestartService(string serviceName, int timeoutMilliseconds)

    {

        ServiceController service = new ServiceController(serviceName);

        try
        {
            int millisec1 = Environment.TickCount;
            TimeSpan timeout = TimeSpan.FromMilliseconds(timeoutMilliseconds);

            service.Stop();
            service.WaitForStatus(ServiceControllerStatus.Stopped, timeout);

            // count the rest of the timeout
            //int millisec2 = Environment.TickCount;
            timeout = TimeSpan.FromMilliseconds(timeoutMilliseconds - millisec1);

            service.Start();
            service.WaitForStatus(ServiceControllerStatus.Running, timeout);
        }

        catch
        {
            // ...
        }

    }