zoukankan      html  css  js  c++  java
  • 用Timer定时发送短信,调用webserver发短信

       protected override void OnStart(string[] args)
            {
                atimer = new System.Timers.Timer(10000);
                atimer.Elapsed += new System.Timers.ElapsedEventHandler(OnTimedEvent);
                atimer.Interval = 1000;
                atimer.Enabled = true;
                Console.ReadLine();
            }

            protected override void OnStop()
            {
                // TODO: 在此处添加代码以执行停止服务所需的关闭操作。
            }
            private void OnTimedEvent(object source, ElapsedEventArgs e)
            {
                atimer.Enabled = false;  //Add by equn 防止该过程被多次执行
                try
                {
                    WFIT.BLL.SendMessageTime bllsendtime = new WFIT.BLL.SendMessageTime();
                    DataTable dt = bllsendtime.GetSendMesTime().Tables[0];
                    DateTime dttest = DateTime.Now;
                    if (dt.Rows.Count > 0)
                    {
                        DateTime SendTime = Convert.ToDateTime(dt.Rows[0]["SendTime"].ToString());
                        DateTime dtnow = DateTime.Now;


                        if (dtnow.ToString("yyyy-MM-dd hh:mm") == SendTime.ToString("yyyy-MM-dd hh:mm"))
                        {
                            foreach (DataRow dr in dt.Rows)
                            {
                                int ApplicationTypeID = Convert.ToInt32(dr["ApplicationTypeID"].ToString());

                                SendMessage(ApplicationTypeID);
                            }

                        }
                        System.TimeSpan ts = SendTime.Subtract(dtnow);
                        if (ts.TotalSeconds > 0)
                        {
                            atimer.Interval = ts.TotalSeconds * 1000;
                        }
                        else
                        {
                            atimer.Interval = 60000;
                        }
                    }
                    else
                    {
                        atimer.Interval = 60000;
                    }
                }
                finally
                {
                    atimer.Enabled = true;
                }

            }
            private DataTable CreateDT()
            {
                DataTable dt = null;
                dt = new DataTable();
                dt.Columns.Add("ReceivePerson", System.Type.GetType("System.String"));
                dt.Columns.Add("PhoneNumber", System.Type.GetType("System.String"));
                return dt;
            }
            private void AddDTRow(string ReceivePerson, string PhoneNumber, ref DataTable dt)
            {
                DataRow dr = null;
                dr = dt.NewRow();
                dr[0] = ReceivePerson;
                dr[1] = PhoneNumber;
                dt.Rows.Add(dr);
            }
            public void SendMessage(int TypeID)
            {
                try
                {
                    //string strbody = "Staff Web短信提醒"+TypeID.ToString();
                    //string telphone = "13760538565";
                    //DataTable dtSentPhone = CreateDT();
                    //AddDTRow("011960", telphone, ref dtSentPhone);
                    //AddDTRow("011960", "13750307426", ref dtSentPhone);
                    //DataSet dtset = new DataSet();
                    //dtset.Tables.Add(dtSentPhone);
                    //SMSServiceApply.SMSServicesSoapClient ss = new WinService.SMSServiceApply.SMSServicesSoapClient();
                    //ss.SendSMSMessageByDefaultAccount(strbody, "", dtset);

                    WFIT.BLL.ApplicationFormAccount bll = new WFIT.BLL.ApplicationFormAccount();
                    DataTable dt = bll.GetHRAlertTable(TypeID);
                    DataTable dtdistinct = dt.DefaultView.ToTable(true, new string[] { "AgentAccount" });

                    if (dtdistinct.Rows.Count > 0)
                    {
                        foreach (DataRow dr in dtdistinct.Rows)
                        {
                            DataRow[] drAccAll = dt.Select("AgentAccount='" + dr["AgentAccount"].ToString() + "'", "IsAgent,ATypeID");
                            int flag1 = 0;
                            int flag2 = 0;
                            string strbody = "[Staff Web提醒]";
                            foreach (DataRow drow in drAccAll)
                            {
                                if (flag1 == 0 && drow["IsAgent"].ToString() == "0")
                                {
                                    strbody += "等待您审批的申请单:";
                                    flag1 = 1;
                                }
                                if (flag2 == 0 && drow["IsAgent"].ToString() == "1")
                                {
                                    strbody += "您可代理审批的申请单:";
                                    flag2 = 1;
                                }
                                strbody += drow["ATypeName"].ToString() + ":" + drow["ACount"].ToString() + "条,";
                            }
                            strbody += "请访问http://staff.wwtt.hk 登录进入个人信息中心审批";

                            WFIT.BLL.WFEmpInfo bllWFE = new WFIT.BLL.WFEmpInfo();
                            WFIT.Model.WFEmpInfo modelWFE = new WFIT.Model.WFEmpInfo();
                            modelWFE = bllWFE.GetModel(dr["AgentAccount"].ToString());
                            if (modelWFE == null || modelWFE.Telphone == null || modelWFE.Telphone == "")
                            {
                                continue;
                            }

                            string telphone = modelWFE.Telphone;
                            //*********lxl添加**********
                            DataTable dtSentPhone = CreateDT();
                            AddDTRow(dr["AgentAccount"].ToString(), telphone, ref dtSentPhone);
                            //AddDTRow("011960", "13750307426", ref dtSentPhone);
                            DataSet dtset = new DataSet();
                            dtset.Tables.Add(dtSentPhone);

                            //************

                            SMSServiceApply.SMSServicesSoapClient ss = new WinService.SMSServiceApply.SMSServicesSoapClient();
                            ss.SendSMSMessageByDefaultAccount(strbody, "", dtset);
                        }
                    }
                }
                catch //(Exception ex)
                {
                    //
                }
            }
            public void start(string[] args)
            {
                this.OnStart(args);
            }
            public void stop()
            {
                this.OnStop();
            }

  • 相关阅读:
    给定一个整数数组和一个目标值,找出数组中和为目标值的两个数 例如给定nums = [2,7,11,15],target = 9
    python的基本语法
    POSIX标准 库文件
    C 标准库头文件
    管理工具:SWOT、PDCA、6W2H、SMART、WBS、时间管理
    函数指针和指针函数的区别
    Linux之正则表达式1
    windows与linux换行规则
    Linux之find
    Linux之文件(目录)默认权限、特殊权限与隐藏权限
  • 原文地址:https://www.cnblogs.com/lgxll/p/2730749.html
Copyright © 2011-2022 走看看