Problem with ScheduledTaskHandler not working

Topics: General, Writing modules
Aug 14, 2012 at 5:44 PM

I am playing around with creating a scheduled task my code is as below

    public class MyTaskHandler : IScheduledTaskHandler
    {

        private const string TaskType = "TestTask";
        private readonly IScheduledTaskManager _taskManager;

        public ILogger Logger { get; set; }

        public MyTaskHandler(IScheduledTaskManager taskManager)
        {
            _taskManager = taskManager;
            Logger = NullLogger.Instance;
            try
            {
                DateTime firstDate = DateTime.Now.AddMinutes(1);
                ScheduleNextTask(firstDate);
            }
            catch (Exception e)
            {
                this.Logger.Error(e.Message);
            }
        }

        public void Process(ScheduledTaskContext context)
        {
            if (context.Task.TaskType == TaskType)
            {
                try
                {
                    this.Logger.Information(string.Format("My task ran at {0}", DateTime.Now.ToString("dd/MM/yy HH:mm:ss")));
                }
                catch (Exception e)
                {
                    this.Logger.Error(e.Message);
                }
                finally
                {
                    DateTime nextTaskDate = DateTime.Now.AddMinutes(10);
                    this.ScheduleNextTask(nextTaskDate);
                }
            }
        }

        private void ScheduleNextTask(DateTime date)
        {
            if (date > DateTime.Now)
            {
                var tasks = _taskManager.GetTasks(TaskType);
                if (tasks == null || tasks.Count() == 0)
                {
                    _taskManager.CreateTask(TaskType, date, null);
                }
            }
        }

    }

It compiles ok - but when I run it i keep getting Cannot access a disposed object.
Object name: 'TransactionScope'

It seems to create the initial entry in the ScheduledTaskRecord though. Out of curiosity I tried it on 1.4.2 - I get no error at all this time - although my Process method never seems to get called.

So what am I getting this error in 1.5.1 and why does the process not get called - any help appreciated

Roland

Aug 20, 2012 at 5:15 PM

Is this not something many people use at the moment?

Aug 21, 2012 at 1:56 PM

Okay feel silly now - of course the date should be the Utc date and as I wasnt passing that it was never handled. Still get the error logged though, but it does run now.