Esempio n. 1
0
 public AsyncJob(AsyncJobHandler job, TimeSpan timeSpan, object state)
 {
     this._Interval     = timeSpan;
     this._Job          = job;
     this._State        = state;
     this._calcelSource = new CancellationTokenSource();
 }
Esempio n. 2
0
 public AsyncJob(AsyncJobHandler job, TimeSpan timeSpan, object state)
 {
     this._Interval = timeSpan;
     this._Job = job;
     this._State = state;
     this._calcelSource = new CancellationTokenSource();
 }
Esempio n. 3
0
        public void LockSlimSampleTest2()
        {
            List <int> list   = new List <int>();
            Random     random = new Random((int)DateTime.Now.Ticks & 100);

            using (var ls = new LockSlim())
            {
                AsyncJobHandler job = j =>
                {
                    using (ls.UpgradeableRead())
                    {
                        for (int i = 0; i < 100; i++)
                        {
                            int item;
                            while (!list.Contains(item = random.Next(0, (int)j.State)))
                            {
                                list.Add(item);
                            }
                        }
                    }
                };
                var job1 = Ajob.Once(job, state: 100);

                var job2 = Ajob.Once(job, state: 200);

                Ajob.Once(j =>
                {
                    job1.Wait();
                    job2.Wait();
                    Assert.Equal(list.Count, list.Distinct().Count());
                    Console.WriteLine(list.Count);
                }).Wait();
            }
        }
Esempio n. 4
0
 internal JobWorker(JobWorkerBuilder builder)
 {
     this.jobWorkerBuilder = builder;
     this.source           = new CancellationTokenSource();
     this.logger           = builder.LoggerFactory?.CreateLogger <JobWorker>();
     this.jobHandler       = jobWorkerBuilder.Handler();
     this.autoCompletion   = builder.AutoCompletionEnabled();
     this.pollInterval     = jobWorkerBuilder.PollInterval();
 }
Esempio n. 5
0
 internal JobWorker(JobWorkerBuilder builder)
 {
     this.jobWorkerBuilder        = builder;
     this.source                  = new CancellationTokenSource();
     this.logger                  = builder.LoggerFactory?.CreateLogger <JobWorker>();
     this.jobHandler              = jobWorkerBuilder.Handler();
     this.autoCompletion          = builder.AutoCompletionEnabled();
     this.pollInterval            = jobWorkerBuilder.PollInterval();
     this.activateJobsCommand     = jobWorkerBuilder.Command;
     this.maxJobsActive           = jobWorkerBuilder.Command.Request.MaxJobsToActivate;
     this.thresholdJobsActivation = maxJobsActive * 0.6;
 }
Esempio n. 6
0
 internal JobWorker(JobWorkerBuilder builder)
 {
     source         = new CancellationTokenSource();
     activator      = new JobActivator(builder.Client);
     activeRequest  = builder.Request;
     maxJobsActive  = activeRequest.MaxJobsToActivate;
     pollInterval   = builder.PollInterval();
     jobClient      = JobClientWrapper.Wrap(builder.JobClient);
     jobHandler     = builder.Handler();
     autoCompletion = builder.AutoCompletionEnabled();
     logger         = builder.LoggerFactory?.CreateLogger <JobWorker>();
 }
Esempio n. 7
0
 /// <summary>
 /// 指定间隔时间重复执行 <paramref name="job"/>。
 /// </summary>
 /// <param name="interval">间隔的时间。</param>
 /// <param name="job">异步任务。</param>
 /// <param name="state">自定义数据。</param>
 /// <returns>异步任务的标识</returns>
 public static IAsyncJob Loop(AsyncJobHandler job, TimeSpan interval, object state = null)
 => new AsyncJob(job, interval, state).Start(LoopInvoke, TaskCreationOptions.LongRunning);
Esempio n. 8
0
 /// <summary>
 /// 指定间隔 <paramref name="milliseconds"/> 毫秒后重复执行 <paramref name="job"/>。
 /// </summary>
 /// <param name="milliseconds">间隔的毫秒数,为 0 时表示立即执行。</param>
 /// <param name="job">异步任务。</param>
 /// <param name="state">自定义数据。</param>
 /// <returns>异步任务的标识</returns>
 public static IAsyncJob Loop(AsyncJobHandler job, double milliseconds = 0, object state = null)
 => Loop(job, TimeSpan.FromMilliseconds(milliseconds), state);
Esempio n. 9
0
 /// <summary>
 /// 指定间隔时间执行一次 <paramref name="job"/>。
 /// </summary>
 /// <param name="interval">间隔的时间。</param>
 /// <param name="job">异步任务。</param>
 /// <param name="state">自定义数据。</param>
 /// <returns>异步任务的标识</returns>
 public static IAsyncJob Once(AsyncJobHandler job, TimeSpan interval, object state = null)
 => new AsyncJob(job, interval, state).Start(OnceInvoke, TaskCreationOptions.PreferFairness);
Esempio n. 10
0
 /// <summary>
 /// 指定 <paramref name="milliseconds"/> 毫秒后执行一次 <paramref name="job"/>。
 /// </summary>
 /// <param name="job">异步任务。</param>
 /// <param name="milliseconds">间隔的毫秒数,为 0 时表示立即执行。</param>
 /// <param name="state">自定义数据。</param>
 /// <returns>返回异步任务的标识</returns>
 public static IAsyncJob Once(AsyncJobHandler job, double milliseconds = 0, object state = null)
 {
     return(Once(job, TimeSpan.FromMilliseconds(milliseconds), state));
 }
Esempio n. 11
0
 public IJobWorkerBuilderStep3 Handler(AsyncJobHandler handler)
 {
     this.asyncJobHandler = handler;
     return(this);
 }
Esempio n. 12
0
 public IJobWorkerBuilderStep3 Handler(JobHandler handler)
 {
     this.asyncJobHandler = (c, j) => Task.Run(() => handler.Invoke(c, j));
     return(this);
 }
Esempio n. 13
0
 /// <summary>
 /// 指定间隔时间重复执行 <paramref name="job"/>。
 /// </summary>
 /// <param name="interval">间隔的时间。</param>
 /// <param name="job">异步任务。</param>
 /// <param name="state">自定义数据。</param>
 /// <returns>异步任务的标识</returns>
 public static IAsyncJob Loop(AsyncJobHandler job, TimeSpan interval, object state = null)
     => new AsyncJob(job, interval, state).Start(LoopInvoke, TaskCreationOptions.LongRunning);
Esempio n. 14
0
 /// <summary>
 /// 指定间隔 <paramref name="milliseconds"/> 毫秒后重复执行 <paramref name="job"/>。
 /// </summary>
 /// <param name="milliseconds">间隔的毫秒数,为 0 时表示立即执行。</param>
 /// <param name="job">异步任务。</param>
 /// <param name="state">自定义数据。</param>
 /// <returns>异步任务的标识</returns>
 public static IAsyncJob Loop(AsyncJobHandler job, double milliseconds = 0, object state = null)
     => Loop(job, TimeSpan.FromMilliseconds(milliseconds), state);
Esempio n. 15
0
 /// <summary>
 /// 指定间隔时间执行一次 <paramref name="job"/>。
 /// </summary>
 /// <param name="interval">间隔的时间。</param>
 /// <param name="job">异步任务。</param>
 /// <param name="state">自定义数据。</param>
 /// <returns>异步任务的标识</returns>
 public static IAsyncJob Once(AsyncJobHandler job, TimeSpan interval, object state = null)
     => new AsyncJob(job, interval, state).Start(OnceInvoke, TaskCreationOptions.PreferFairness);