1

(Edit) I'm not asking what a NullReferenceException is. This is not a duplicate. The issue is regarding System.Task...


I have a Windows service running on Server 2003 SP2 that has been running fine for over a year. The service just runs a timer and launches various tasks throughout the day. Several of these tasks are long-running processes, so the service uses a System.Task to launch the long-running tasks so they don't block the main timer thread. Some of the task now fail every time, right after they are started. All tasks launch and run fine in the VS2013 IDE, but fail when deployed in the server.

This is the code from the Windows service timer elapsed method that is hit when it's time to run the async task:

if (!IsTaskRunning(processConfig.Name)) {   _log.Write("Executing process " + processConfig.Name + "(" + occurance.Name + ") asyncronously.");
        AsyncTask task = new AsyncTask(processConfig, occurance, Task<ProcessResult>.Factory.StartNew(() => process.Run()), processConfig.Name);
        _asyncTasks.Add(task); }

The AsyncTask object is my own creation that just wraps the System.Task object with a few other pieces of information needed to report the task status when it finishes. The task fails immediately after creation (it starts execution immediately). This is the exception and stack trace:

System.AggregateException: One or more errors occurred. ---> System.NullReferenceException: Object reference not set to an instance of an object. at OnePoint.Automation.ServiceBase.<>c__DisplayClass6.b__3() at System.Threading.Tasks.Task1.InvokeFuture(Object futureAsObj)
at System.Threading.Tasks.Task.InnerInvoke() at System.Threading.Tasks.Task.Execute() --- End of inner exception stack trace --- ---> (Inner Exception #0) System.NullReferenceException: Object reference not set to an instance of an object. at OnePoint.Automation.ServiceBase.<>c__DisplayClass6.<CheckForTasksToRun>b__3() at System.Threading.Tasks.Task
1.InvokeFuture(Object futureAsObj)
at System.Threading.Tasks.Task.InnerInvoke() at System.Threading.Tasks.Task.Execute()<---

I have tried other methods of starting the task, ie. instantiating the task and AsyncTask object, then calling Task.Start() after, but the behavior is the same. I even placed logging inside the actual process.Run() method to see if the code ever gets reached, and it does not.

Any tips/help would be appreciated. This problem has wasted so much of my time already.

Jan Doggen
  • 8,799
  • 13
  • 70
  • 144
pmcb
  • 349
  • 2
  • 7
  • Software doesn't spoil. Something must have changed on the Windows server or with the (service) account under which your service runs. Check your recent updates and your account privileges. Your issue may not be obvious outright but may yield a clue. – Paul Sasik Aug 08 '14 at 00:47
  • What is OnePoint.Automation.ServiceBase ? – Mick Aug 08 '14 at 00:59
  • The ServiceBase is a class that is the entry point for the application. It's what is instantiated and launched by the Windows service. Basically it just runs the timer and monitors for tasks to run. – pmcb Aug 08 '14 at 11:37
  • Unfortunately I don't have control over the server/patches deployed. I will check it out though. – pmcb Aug 08 '14 at 13:33
  • FYI I ended up scrapping the Task based implementation and using standard threading implementation instead. I wasted too much time trying to log and debug the original implementation, and I needed to get things working for my client. Instead of launching a Task, I opted to just use System.Threading.Thread to run the process. – pmcb Apr 22 '15 at 03:08

0 Answers0