Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run a task when a windows service starts?

I have a windows service and I've written the code to run the task within the OnStart() event:

 protected override void OnStart(string[] args)
        {
            this.DoTask();
        }

private void DoTask()
        {
            Task task1 = Task.Factory.StartNew(() => this.OriginalFileProcessor.StartPolling());

            try
            {
                Task.Wait(task1);
            }
            catch (Exception ex)
            {
                this.Log.Error("Failed running the task", ex);
            }           
        }

The DoTask is a never-ending loop. It will stop only when the service is stopped.

But when I try to start the service, it waits a long time then gives me the below error:

Windows could not start the ... service on Local Computer.
Error 1053: The service did not respond to the start or control request in a timely fashion.

How to resolve it?

like image 599
The Light Avatar asked Apr 12 '13 14:04

The Light


People also ask

How do I automate a Windows service restart?

From the desktop, click Start > Control Panel. Double-click Administration Tools. Double-click NetIQ Operations Center Auto-Restart Service. The Auto-Restart service automatically starts when Windows starts.

How do I schedule a service in Windows 10?

Open Start, Search for "Task Scheduler" and press enter to open "Task Scheduler". Right-click on the "Task Scheduler Library" and click on the "New Folder" option. Enter the name of the New folder and click on the "OK" button. Navigate the following: Task Scheduler Library > New Folder, then click on "Create Task".

How do I start Task Scheduler service from command line?

Press the Windows + R keys on your keyboard to open Run, and then type taskschd. msc in the Open field. Finally, click or tap on OK, or press Enter on your keyboard.

What is the Task Scheduler service?

The Task Scheduler service allows you to perform automated tasks on a chosen computer. With this service, you can schedule any program to run at a convenient time for you or when a specific event occurs.


3 Answers

Why are you waiting your task to finish?

I think Task.Wait is blocking your current thread, then you're getting timeout while starting your service.

EDIT: You need to remove this block:

try
{
    Task.Wait(task1);
}
catch (Exception ex)
{
    this.Log.Error("Failed running the task", ex);
}  

Task.Wait is indeed blocking your current thread. According to MSDN:

Task.Wait Method

Waits for the Task to complete execution.

EDIT 2 Do this instead

Task task1 = Task.Factory.StartNew(() => this.OriginalFileProcessor.StartPolling()).ContinueWith( t =>
{
     var aggException = t.Exception.Flatten();
     foreach(var ex in aggException.InnerExceptions)
         this.Log.Error("Failed running the task", ex);
}, 
TaskContinuationOptions.OnlyOnFaulted);
like image 153
Conrad Clark Avatar answered Oct 18 '22 19:10

Conrad Clark


I guess this is, because you are waiting for the OriginalFileProcessor.StartPolling() to end, but this never happens. You should move your task instance into an seperate member and not wait for it to finish:

private Task m_task = null;

private void DoTask()
{
    try
    {
        m_task = Task.Factory.StartNew(() => this.StartPolling());
    }
    catch
    {
        this.Log.Error("Unable to start task", ex);
        throw;  // Rethrow, so that the OS knows, there was something wrong.
    }           
}

private void StartPolling()
{
    try
    {
        this.OriginalFileProcessor.StartPolling();
    }
    catch (Exception ex)
    {
        this.Log.Error("Failed running the task", ex);
    }
}
like image 2
Carsten Avatar answered Oct 18 '22 20:10

Carsten


In the loop you need to check if the service status is "stopping" and exit the loop. You have 5 seconds to do that before the OS decides to kill you.

like image 1
Z.D. Avatar answered Oct 18 '22 19:10

Z.D.