Ejemplo n.º 1
0
        public async static void Run()
        {
            try
            {
                string cronExpression = "0/1 * * * * ?";//这是指每天的9点和16点执行任务
                QuartzUtil.QuartzUtilSet();
                await QuartzUtil.AddJob <QuartzJob>("job1", cronExpression);

                Console.WriteLine("执行成功");
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            // QuartzUtil.ExecuteByCron<QuartzJob>(cronExpression);//这是调用Cron计划方法
        }
        /// <summary>
        /// 开启或者关闭任务
        /// </summary>
        /// <param name="userId">用户ID</param>
        /// <param name="newJobStatedict"></param>
        private void OpenJob(string userId, Dictionary <string, JobState> newJobStatedict)
        {
            Dictionary <string, string> dict = _cache.GetCache <Dictionary <string, string> >("__ConnectionUserCacheKey");

            if (dict.Count > 0)
            {
                foreach (KeyValuePair <string, JobState> pair in newJobStatedict)
                {
                    string jobCode = pair.Key;
                    switch (pair.Value)
                    {
                    case JobState.Open:
                        //开启任务,操作数据,然后将数据广播给指定用户
                        //Cron表达式 :秒  分钟  小时  日的日  月  某一天的周  年
                        //每分钟执行
                        string CronTime = "0/5 * * * * ? ";
                        //附带参数
                        JobDataMap map;

                        switch (jobCode)
                        {
                        case "SysJob":
                            map = new JobDataMap {
                                { "Clients", Clients }
                            };
                            DateTimeOffset time = QuartzUtil.AddJob <SysJob>(jobCode, CronTime, map);
                            break;
                        }
                        //向客户端广播消息
                        if (dict.ContainsKey(userId))
                        {
                            string connId = dict[userId];
                            Clients.Client(connId).BroadcastJobOpened(jobCode + "已开启");
                        }
                        break;

                    case JobState.Closed:
                        break;
                    }
                }
            }
        }