private int GetMsToTask(Time now, Task task)
        {
            int msToTask;
            bool isWithinTaskTime = false;
            int taskTimeCompare = TimeFuncs.CompareTime(task.MonitorStartTime, task.MonitorEndTime);
            int nowStartTimeCompare = TimeFuncs.CompareTime(now, task.MonitorStartTime);
            int nowEndTimeCompare = TimeFuncs.CompareTime(now, task.MonitorEndTime);

            if (taskTimeCompare < 0) // start is smaller than end
            {
                if (nowStartTimeCompare >= 0 && nowEndTimeCompare <= 0)
                {
                    isWithinTaskTime = true;
                }
            }
            else // start is bigger than end (or equal)  - monitor time across two days
            {
                if ((nowStartTimeCompare > 0 && nowEndTimeCompare > 0)
                    || (nowStartTimeCompare < 0 && nowEndTimeCompare < 0))
                {
                    isWithinTaskTime = true;
                }
            }

            if (isWithinTaskTime)
            {
                msToTask = 0;
            }
            else
            {
                //if (nowStartTimeCompare <= 0)
                //{
                //    // Task will start later today
                //    msToTask = TimeFuncs.HoursToMilliseconds(task.MonitorStartTime.Hour - now.Hour);
                //}
                //else
                //{
                //    // Task will only start "tomorrow"
                //    msToTask = TimeFuncs.HoursToMilliseconds((23 - now.Hour) + task.MonitorStartTime.Hour);
                //}
                msToTask = TimeFuncs.GetMsFromTo(now, task.MonitorStartTime);
            }

            return msToTask;
        }
        private int LoadTasks()
        {
            lock (_taskMonitorList)
            {
                Globals.WriteToDebugFile("TaskMonitor: Loading Tasks");
                int msToNext = 86400000; //24h * 60m * 60s * 1000 <- the max wait time is 24h
                Time now = new Time(DateTime.Now);

                LSTasksConfig tasksCfg = TasksLoader.LoadTasksFromFile();
                _updateRate = tasksCfg.UpdateRate * 1000;
                _positionTools.UseCellLocation = tasksCfg.UseCellLocation;
                List<Task> taskList = tasksCfg.TaskList;

                foreach (Task task in taskList)
                {
                    TasksMonitorTask tmt = new TasksMonitorTask();
                    tmt.task = task;
                    tmt.msToStart = GetMsToTask(now, task);
                    _taskMonitorList.Add(tmt);
                    if (tmt.msToStart < msToNext) msToNext = tmt.msToStart.Value;
                }
                _lastUpdate = now;
                return msToNext;
            }
        }
        private int ProcessTasks()
        {
            int msToNext = 86400000; //24h * 60m * 60s * 1000 <- the max wait time is 24h
            Time now = new Time(DateTime.Now);
            int msFromLastUpd = TimeFuncs.GetMsFromTo(_lastUpdate, now);
            Coordinates currentPos = _positionTools.GetCurrentPosition(_updateRate);

            foreach (TasksMonitorTask tmt in _taskMonitorList)
            {
                tmt.msToStart -= msFromLastUpd;

                if (tmt.msToStart <= 0)
                {
                    tmt.msToStart = GetMsToTask(now, tmt.task);

                    // re-check
                    if (tmt.msToStart <= 0)
                    {
                        if (currentPos.IsValid())
                        {
                            if (ProcessTask(tmt.task, currentPos))
                            {
                                tmt.msToStart = TimeFuncs.GetMsFromTo(now, tmt.task.MonitorStartTime);
                            }
                        }
                    }
                }

                if (msToNext > tmt.msToStart.Value)
                {
                    msToNext = tmt.msToStart.Value;
                }
            }

            if (msToNext < _updateRate)
            {
                msToNext = _updateRate;
            }

            _lastUpdate = now;
            return msToNext;
        }