Пример #1
0
        public void Run()
        {
            var scheduler = new WebJobScheduler((cancellation) => {
                Parallel.ForEach(this.workItems, new ParallelOptions()
                {
                    MaxDegreeOfParallelism = 5
                }, (workitem) => {
                    while (cancellation.IsCancellationRequested == false)
                    {
                        try {
                            var offset = 60D;                            //10 minutes
                            workitem.Execute();
                            for (var i = 0; ((cancellation.IsCancellationRequested == false) && (i < offset)); i++)
                            {
                                Thread.Sleep(1000);
                            }
                        } catch (Exception ex) {
                            Logger.Error(ex.Message, ex);
                        }
                    }
                });
            });

            scheduler.Shutdown += (sender, xx) => {
                foreach (var workitem in this.workItems)
                {
                    workitem.Abort();
                }
            };
            scheduler.Start();
        }
Пример #2
0
        static void StartSyncHistory()
        {
            var workitems = CitizensHost.GetServices <ICitizensWorkItem>();
            var scheduler = new WebJobScheduler((cancellation) =>
            {
                Parallel.ForEach(workitems, new ParallelOptions()
                {
                    MaxDegreeOfParallelism = 5
                }, (workitem) =>
                {
                    try
                    {
                        workitem.Execute();
                    }
                    catch (Exception ex)
                    {
                        Logger.Error(ex.Message, ex);
                    }
                });
            });

            scheduler.Shutdown += (sender, args) =>
            {
                foreach (var workitem in workitems)
                {
                    workitem.Abort();
                }
            };
            scheduler.Start();
            Console.Read();
        }
Пример #3
0
        static void StartAuto()
        {
            var workitems = IoC.GetServices <IWorkItem>();
            var scheduler = new WebJobScheduler((cancellation) =>
            {
                Parallel.ForEach(workitems, new ParallelOptions()
                {
                    MaxDegreeOfParallelism = 5
                }, (workitem) =>
                {
                    while (cancellation.IsCancellationRequested == false)
                    {
                        try
                        {
                            var offset = 60D * 10;//1 hour
                            workitem.Execute();
                            for (var i = 0; ((cancellation.IsCancellationRequested == false) && (i < offset)); i++)
                            {
                                Thread.Sleep(1000);
                            }
                        }
                        catch (Exception ex)
                        {
                            Logger.Error(ex.Message, ex);
                        }
                    }
                });
            });

            scheduler.Shutdown += (sender, args) =>
            {
                foreach (var workitem in workitems)
                {
                    workitem.Abort();
                }
            };
            scheduler.Start();
        }