ArgusTV.Recorder.MediaPortalTvServer.DvbEpgThread.Run C# (CSharp) Method

Run() protected method

protected Run ( ) : void
return void
        protected override void Run()
        {
            Thread.Sleep(5 * 1000);

            lock (_guideProgramsToImportLock)
            {
                _newProgramsToImportEvent = new AutoResetEvent(false);
            }
            try
            {
                int interval = 1 * 60 * 1000;
            #if DEBUG
                interval = 5000;
            #endif

                bool aborted = false;
                while (!aborted)
                {
                    try
                    {
                        List<GuideProgram> guidePrograms = GetProgramsToImport();
                        while (guidePrograms != null)
                        {
                            using (GuideServiceAgent tvGuideAgent = new GuideServiceAgent())
                            {
                                Log.Debug("ArgusTV.Recorder.MediaPortalTvServer: ArgusTVDvbEpg: importing {0} programs into ARGUS TV", guidePrograms.Count);
                                foreach (GuideProgram guideProgram in guidePrograms)
                                {
                                    tvGuideAgent.ImportProgram(guideProgram, GuideSource.DvbEpg);
                                    aborted = this.StopThreadEvent.WaitOne(0, false);
                                    if (aborted)
                                    {
                                        break;
                                    }
                                }
                            }
                            aborted = this.StopThreadEvent.WaitOne(0);
                            if (aborted)
                            {
                                break;
                            }
                            guidePrograms = GetProgramsToImport();
                        }
                        if (!aborted)
                        {
                            aborted = (0 == WaitHandle.WaitAny(new WaitHandle[] { this.StopThreadEvent, _newProgramsToImportEvent }, interval, false));
                        }
                    }
                    catch (Exception ex)
                    {
                        Log.Error("ArgusTVDvbEpg error: {0}", ex.Message);
                        // Delay for a short while and then restart.
                        aborted = this.StopThreadEvent.WaitOne(30 * 1000, false);
                    }
                }
            }
            finally
            {
                lock (_guideProgramsToImportLock)
                {
                    _newProgramsToImportEvent.Close();
                    _newProgramsToImportEvent = null;
                }
            }
        }