Global定时器任务使用。.NET定时执行方法

  • A+
所属分类:.NET技术
摘要

  

using System; using System.Collections.Generic; using System.Linq; using System.Threading; using System.Web; using System.Web.Optimization; using System.Web.Routing; using System.Web.Security; using System.Web.SessionState; using BLL; using DAL; using DingTalk.Api.Response; using Model; using Newtonsoft.Json.Linq; using DingTalk.Api; using DingTalk.Api.Request; //using MyWeb.Common; using static DingTalk.Api.Response.OapiDepartmentListResponse; using System.Text; using System.Data; using System.Data.SqlClient; using static DingTalk.Api.Response.OapiAttendanceListscheduleResponse; using Newtonsoft.Json; using static DingTalk.Api.Response.OapiProcessinstanceGetResponse; using System.Configuration; using System.Net; using System.IO;  namespace MyProject {     public class Global : HttpApplication     {                   void Application_Start(object sender, EventArgs e)         {             // 在应用程序启动时运行的代码。更新完,打开http://m.91zn.cn:81/AmebaScore/Test01.aspx激活一下应用程序池              Thread t1 = new Thread(new ThreadStart(time001));             t1.Start();              Thread t2 = new Thread(new ThreadStart(time002));             t2.Start();              Thread t3 = new Thread(new ThreadStart(time003));             t3.Start();                      }          #region 定时器---获取钉钉全部通讯录人员         private void time001()         {             System.Timers.Timer MyTimerGetdingdingUser = new System.Timers.Timer();             //MyTimerGetdingdingUser.Elapsed += GetMyUser;             MyTimerGetdingdingUser.Interval = 1000 * 60 * 60;//每天触发一次1000是一秒             //MyTimerGetdingdingUser.Interval = 1000 * 120;//每天触发一次1000是一秒             MyTimerGetdingdingUser.Enabled = true;             MyTimerGetdingdingUser.AutoReset = true;             MyTimerGetdingdingUser.Start();             MyTimerGetdingdingUser.Elapsed += new System.Timers.ElapsedEventHandler(GetMyUser);          }         #endregion          #region 定时器---同步HIS上开检验单数据到LIS瑞美         private void time002()         {              System.Timers.Timer MyTimerTongBuHisLis = new System.Timers.Timer();             MyTimerTongBuHisLis.Elapsed += TongBuHisLis;             //MyTimerTongBuHisLis.Interval = 1000 * 60 * 60 * 24;//每天触发一次1000是一秒             MyTimerTongBuHisLis.Interval = 1000 * 10;//每天触发一次1000是一秒             MyTimerTongBuHisLis.Enabled = true;//false             MyTimerTongBuHisLis.AutoReset = true;           }         #endregion          #region 定时器---同步钉钉排班         private void time003()         {                          System.Timers.Timer MyTimerTongBuPaiBan = new System.Timers.Timer();             MyTimerTongBuPaiBan.Elapsed += TongBuDDPaiBan;             MyTimerTongBuPaiBan.Interval = 1000 * 60 * 30;//每小时触发一次1000是一秒             //MyTimerTongBuPaiBan.Interval = 1000 * 200;//每天触发一次1000是一秒             MyTimerTongBuPaiBan.Enabled = true;//false             MyTimerTongBuPaiBan.AutoReset = true;          }         #endregion          #region 定时器---获取钉钉全部通讯录人员          void GetMyUser(object sender, System.Timers.ElapsedEventArgs e)         {             TimerTask.GetMyUser();         }         #endregion                  #region 同步HIS上开检验单数据到LIS瑞美         void TongBuHisLis(object sender, System.Timers.ElapsedEventArgs e)         {             // AND DATEDIFF(dd,t1.sqsj,GETDATE()) = 0              StringBuilder sbhcsqh = new StringBuilder();             string sql = @"                  SELECT t2.sqh,t2.brly,0 AS ifjz ,t2.brdh,t2.brxm,CASE brxb WHEN '男' THEN '1' ELSE '2' END AS patsex,brsr,ch,sqys,ksdh,zd,'' AS testname,t3.item,0 AS mode 	                FROM Lis_SQH t1 	                INNER JOIN SQLHIS.dbo.v_HIS_Lis_pat t2 ON t1.sqh=t2.sqh 	                INNER JOIN  	                ( 		                SELECT sqh 		                 ,item = stuff(( 			                SELECT xmdh + ',' + XMMC + ',' + convert(varchar(50),dj) + ',' + convert(varchar(50),sl) + '|' 			                FROM SQLHIS.dbo.v_HIS_Lis_pat_mx 			                WHERE sqh = a.sqh 			                FOR XML path('') 		                ), 1, 0, '') 		                FROM SQLHIS.dbo.v_HIS_Lis_pat_mx a	--需要同步的申请单的项目的明细的视图@item ='501404,孕酮P,50,1|501396,雌二醇,50,1|' 		                GROUP BY sqh 	                ) t3 ON t2.sqh=t3.sqh 	                WHERE t1.state=0  	 	                UNION ALL 	 	                SELECT t2.sqh,t2.brly,0 AS ifjz,t2.brdh,t2.brxm,CASE brxb WHEN '男' THEN '1' ELSE '2' END AS patsex,brsr,ch,sqys,ksdh,zd,'' AS testname,t3.item,0  AS mode 	                FROM Lis_SQH t1 	                INNER JOIN SQLHIS_mq.dbo.v_HIS_Lis_pat t2 ON t1.sqh=t2.sqh 	                INNER JOIN  	                ( 		                SELECT sqh 		                 ,item = stuff(( 			                SELECT xmdh + ',' + XMMC + ',' + convert(varchar(50),dj) + ',' + convert(varchar(50),sl) + '|' 			                FROM SQLHIS_mq.dbo.v_HIS_Lis_pat_mx 			                WHERE sqh = a.sqh 			                FOR XML path('') 		                ), 1, 0, '') 		                FROM SQLHIS_mq.dbo.v_HIS_Lis_pat_mx a	--需要同步的申请单的项目的明细的视图@item ='501404,孕酮P,50,1|501396,雌二醇,50,1|' 		                GROUP BY sqh 	                ) t3 ON t2.sqh=t3.sqh 	                WHERE t1.state=0                  ";             DataTable table = SqlHelperSQLHIS.ExecuteReader(sql, CommandType.Text);             if (table == null || table.Rows.Count == 0)             {             }             else             {                 //v_HIS_Lis_patModel v_HIS_Lis_patModel = null;                 int ire = 0;                 for (int i = 0; i < table.Rows.Count; i++)                 {                     sbhcsqh.Append("'" + table.Rows[i]["sqh"] + "',");//把此次同步的申请单号存起来,作为回改state=1用                     //v_HIS_Lis_patModel.DingdingUserid = table.Rows[i]["dingdingUserid"] == DBNull.Value ? "" : (string)table.Rows[i]["dingdingUserid"];                     //v_HIS_Lis_patModel = new v_HIS_Lis_patModel();                     SqlParameter[] parameters = new SqlParameter[]                     {                         new SqlParameter("@applyno", SqlDbType.VarChar, 500),                         new SqlParameter("@pattype", SqlDbType.VarChar, 500),                         new SqlParameter("@ifjz", SqlDbType.Int, 50),                         new SqlParameter("@patno", SqlDbType.VarChar, 500),                         new SqlParameter("@patname", SqlDbType.VarChar, 500),                         new SqlParameter("@patsex", SqlDbType.VarChar, 500),                         new SqlParameter("@patbrith", SqlDbType.DateTime, 500),                         new SqlParameter("@bedno", SqlDbType.VarChar, 500),                         new SqlParameter("@docna", SqlDbType.VarChar, 500),                         new SqlParameter("@depno", SqlDbType.VarChar, 500),                         new SqlParameter("@dianose", SqlDbType.VarChar, 500),                         new SqlParameter("@testname", SqlDbType.VarChar, 500),                         new SqlParameter("@item", SqlDbType.NText, 4000),                         new SqlParameter("@mode", SqlDbType.Int, 20)                     };                     parameters[0].Value = table.Rows[i]["sqh"];                     parameters[1].Value = table.Rows[i]["brly"];                     parameters[2].Value = Convert.ToInt32(table.Rows[i]["ifjz"]);                     parameters[3].Value = table.Rows[i]["brdh"];                     parameters[4].Value = table.Rows[i]["brxm"];                     parameters[5].Value = table.Rows[i]["patsex"];                     parameters[6].Value = Convert.ToString(table.Rows[i]["brsr"]);                     parameters[7].Value = Convert.ToString(table.Rows[i]["ch"]);                     parameters[8].Value = table.Rows[i]["sqys"];                     parameters[9].Value = table.Rows[i]["ksdh"];                     parameters[10].Value = table.Rows[i]["zd"];                     parameters[11].Value = Convert.ToString(table.Rows[i]["testname"]);                     parameters[12].Value = table.Rows[i]["item"];                     parameters[13].Value = Convert.ToInt32(table.Rows[i]["mode"]);                     ire = SqlHelperLis2002ywq.ExecuteNonQuery("sp_lisapp", CommandType.StoredProcedure, parameters);                   }                 if (ire > 0)                 {                     //回改sbhcsqh里的state=1                     string strhgsql = "UPDATE lis_sqh SET state=1 WHERE sqh IN(" + sbhcsqh.ToString().Substring(0, sbhcsqh.ToString().Length - 1) + ")";                     SqlHelperSQLHIS.ExecuteNonQuery(strhgsql, CommandType.Text);                 }             }         }         #endregion                  #region 同步钉钉排班         void TongBuDDPaiBan(object sender, System.Timers.ElapsedEventArgs e)         {             TimerTask.TongBuDDPaiBan();         }         #endregion                  #region 临时类          /// <summary>         /// 返回给ajax请求的结果类         /// </summary>         private class ResultInfo         {             /// <summary>             /// 是否成功, true:成功 false:失败             /// </summary>             public bool State { get; set; }             /// <summary>             /// 返回的数据集             /// </summary>             public object Data { get; set; }             /// <summary>             /// 成功信息             /// </summary>             public string Message { get; set; }             /// <summary>             /// 错误信息             /// </summary>             public string ErrorMsg { get; set; }             /// <summary>             /// 当前第几页             /// </summary>             public int PageIndex { get; set; }             /// <summary>             /// 记录总数             /// </summary>             public int RecordCount { get; set; }         }         private class userid02         {             /// <summary>             /// 员工工号             /// </summary>             public string Jobnumber { get; set; }             /// <summary>             /// 钉ID             /// </summary>             public string Userid { get; set; }         }            #endregion          protected void Application_End(object sender, EventArgs e)         {              SqlHelperSQLHIS.ExecuteNonQuery(" INSERT INTO a_test (cont) VALUES ('程序回收了') ", CommandType.Text);              //下面的代码是关键,可解决IIS应用程序池自动回收的问题               Thread.Sleep(1000);              //这里设置你的web地址,可以随便指向本项目你的任意一个aspx页面甚至不存在的页面,目的是要激发Application_Start               string url = "http://XXXXX/Test01.aspx";             HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create(url);             HttpWebResponse myHttpWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse();             Stream receiveStream = myHttpWebResponse.GetResponseStream();//得到回写的字节流               SqlHelperSQLHIS.ExecuteNonQuery(" INSERT INTO a_test (cont) VALUES ('程序自动重新启动了') ", CommandType.Text);         }                       } }