Ejemplo n.º 1
0
 public async void StartBackgroundJobs()
 {
     AddBackgroundInformation("Background jobs", "Starting background jobs");
     if (BackgroundJobs == null)
     {
         try
         {
             InitializeBackgroundJobs();
         }
         catch (Exception e)
         {
             AddBackgroundError("Starting error", e);
         }
     }
     //AddBackgroundInformation("Background jobs", "Starting background jobs 2");
     foreach (var backgroundJob in BackgroundJobs)
     {
         //var thread = new Thread(new ParameterizedThreadStart(RunBackgroundEventLoop));
         var task = Task.Run(async() => RunBackgroundEventLoop(backgroundJob));
         BackgroundThreads.Add(task);
         //thread.Start(backgroundJob);
     }
     BackgroundManager.StartWorkers(); // starts background timers that process jobs in the queue
     AddBackgroundInformation("Background jobs", "Background jobs started");
 }
Ejemplo n.º 2
0
        static void Main(string[] args)
        {
            IExample background = new BackgroundThreads();
            // background.Run();

            IExample parametrized = new ParametrizedThread();
            // parametrized.Run();

            IExample threadStatic = new ThreadStatic();

            // threadStatic.Run();

            // new ThreadPoolExample().Run();
            new ParallelExample().Run();
        }
Ejemplo n.º 3
0
        private async void StartBackgroundJobs_Internal()
        {
            AddBackgroundInformation("Background jobs", "Starting background jobs");
            if (BackgroundJobs == null)
            {
                try
                {
                    InitializeBackgroundJobs();
                }
                catch (Exception e)
                {
                    AddBackgroundError("Starting error", e);
                }
            }

            foreach (var backgroundJob in BackgroundJobs)
            {
                var thread = new Thread(new ParameterizedThreadStart(BackgroundWork));
                BackgroundThreads.Add(thread);
                thread.Start(backgroundJob);
            }
            AddBackgroundInformation("Background jobs", "Backgound jobs started");
        }
Ejemplo n.º 4
0
        private void BackgroundWork(object jobObject)
        {
            var job       = (BackgroundJob)jobObject;
            var firstTime = true;

            try
            {
                while (true)
                {
                    if (firstTime && job.Event.RunImmediatelyFirstTime)
                    {
                        firstTime = false;
                    }
                    else
                    {
                        /* First calculate the amount of time to wait before doing work */
                        job.NextRunTime = job.Event.CalculateNextRunTime(job.LastRunTime);
                        var sleepTime = job.NextRunTime.Subtract(DateTime.Now);
                        AddBackgroundInformation(job.Event.Description, String.Format("Background process {0} is going to sleep for {1} days, {2} hours, {3} minutes and {4} seconds", job.Event.Description, sleepTime.Days, sleepTime.Hours, sleepTime.Minutes, sleepTime.Seconds));
                        Thread.Sleep(sleepTime);
                    }

                    var result = new BackgroundJobResult()
                    {
                        EventNumber    = job.Event.GetEventId(),
                        DateTimeRunUTC = DateTime.UtcNow
                    };
                    job.LastRunTime = DateTime.Now;
                    try
                    {
                        if (BackupService.BusyWithBackups == true)
                        {
                            continue;
                        }
                        job.Event.DoWork();
                        result.Status = "Success";
                    }
                    catch (Exception e)
                    {
                        //result.Status = "Error";
                        //result.ExecutionInformation = e.Message + "\n" + e.StackTrace;
                        //AddBackgroundError(job.Event.Description, e);
                        throw;
                        //TODO: Log this in file and in a way to display on screen. Maybe i can  do both in 1
                    }
                    AddBackgroundInformation(job.Event.Description, String.Format("Ran background process {0} : {1} -> {2}", job.Event.Description, result.Status, result.ExecutionInformation));

                    //SaveBackgroundJobResult(result);
                }
            }
            catch (Exception error)
            {
                AddBackgroundError("Doing BackgroundWork", error);

                // Try fix background stopping
                Started = false;

                ///if (error is ThreadAbortException)
                {
                    //Thread.CurrentThread.Abort();
                    var index = BackgroundThreads.IndexOf(Thread.CurrentThread);
                    if (index > -1)
                    {
                        var backgroundJob = BackgroundJobs[index];
                        var thread        = new Thread(new ParameterizedThreadStart(BackgroundWork));
                        BackgroundThreads.Add(thread);
                        thread.Start(backgroundJob);

                        BackgroundThreads[index] = thread;
                    }
                }

                //Thread.Sleep(TimeSpan.FromSeconds(1));
                //BackgroundWork(jobObject);
            }
        }