当前位置: 代码迷 >> ASP.NET >> 计划任务解决思路
  详细解决方案

计划任务解决思路

热度:9602   发布时间:2013-02-25 00:00:00.0
计划任务
我在网上搜索了很多资料
关于说asp.net的计划任务
因为都是租的别人的虚拟空间
所以不可能使用windows计划任务
我也试着写了一个计划任务,可是没有实现,请大家给指点一下
代码如下:
C# code
<script runat="server">    DateTime a ;    DateTime b ;    DateTime c = Convert.ToDateTime(DateTime.Now.TimeOfDay.ToString());    int Minute = (int)System.DateTime.Now.Minute;    void Application_Start(object sender, EventArgs e)     {        // 在应用程序启动时运行的代码             //定义定时器     //1000表示1秒的意思             System.Timers.Timer timer = new System.Timers.Timer(1000 * 60);        timer.Elapsed += new System.Timers.ElapsedEventHandler(_采集数据);        timer.Enabled = true;        timer.AutoReset = true;    }    void _采集数据(object sender, System.Timers.ElapsedEventArgs e)    {        //采集山东开奖数据        a = Convert.ToDateTime("09:05:00.0000000");        b = Convert.ToDateTime("22:00:00.0000000");        string[] sd = _采集开奖号码(url_sd);        if (c > a && c < b && Minute % 10 == 8)            _插入开奖数据("山东", sd[0], sd[1] + " " + sd[2] + " " + sd[3] + " " + sd[4] + " " + sd[5], " ");        //采集重庆开奖数据        string[] cq = _采集开奖号码(url_cq);        a = Convert.ToDateTime("09:00:00.0000000");        b = Convert.ToDateTime("23:05:00.0000000");        if (c > a && c < b && Minute % 10 == 3)            _插入开奖数据("重庆", cq[0], cq[1] + " " + cq[2] + " " + cq[3] + " " + cq[4] + " " + cq[5], " ");        //采集江西开奖数据        a = Convert.ToDateTime("09:12:00.0000000");        b = Convert.ToDateTime("22:05:00.0000000");        string[] jx = _采集开奖号码(url_jx);        if (c > a && c < b && Minute % 12 == 2)            _插入开奖数据("江西", jx[0], jx[1] + " " + jx[2] + " " + jx[3] + " " + jx[4] + " " + jx[5], " ");        //采集广东开奖数据        string[] gd = _采集开奖号码(url_gd);        a = Convert.ToDateTime("09:12:00.0000000");        b = Convert.ToDateTime("23:05:00.0000000");        if (c > a && c < b && Minute % 12 == 2)            _插入开奖数据("广东", gd[0], gd[1] + " " + gd[2] + " " + gd[3] + " " + gd[4] + " " + gd[5], " ");    }    public string url_sd = "http://www.kuaicaile.com/sd11y/index.jhtml";    public string url_gd = "http://www.kuaicaile.com/gd11x5/index.jhtml";    public string url_jx = "http://www.kuaicaile.com/jxdlc/index.jhtml";    public string url_cq = "http://www.kuaicaile.com/cq11x5/index.jhtml";    public string[] _采集开奖号码(string url)    {        string[] datas; //用于存储期号及5位开奖数据        string substring1 = "justOpenIssueNo='";        int a1 = 17;        int b1 = 14;        string substring2 = "justOpenIssue='";        int a2 = 15;        int b2 = 8;        //获取5位开奖数据        System.Net.WebClient wc = new System.Net.WebClient();        string data = Encoding.UTF8.GetString(wc.DownloadData(string.Format(url)));        data = data.Substring(data.IndexOf(substring1) + a1, b1);        string[] strs = data.Split(',');        Array.Sort(strs);        //获取期号        string qihao = Encoding.UTF8.GetString(wc.DownloadData(string.Format(url)));        qihao = qihao.Substring(qihao.IndexOf(substring2) + a2, b2);        qihao = "20" + qihao;        datas = new string[] { qihao, strs[0].ToString(), strs[1].ToString(), strs[2].ToString(), strs[3].ToString(), strs[4].ToString() };        return datas;    }    public void _插入开奖数据(string city, string qihao, string data, string Bool)    {        System.Data.SqlClient.SqlConnection conn;        System.Data.SqlClient.SqlCommand cmd;        string connectionstring = System.Web.Configuration.WebConfigurationManager.ConnectionStrings["sqli08kingdomConnectionString"].ConnectionString;        string cmdString = string.Format("INSERT INTO [{0}开奖数据] (期号,开奖号码,对错) VALUES('{1}','{2}','{3}')", city, qihao, data, Bool);        conn = new System.Data.SqlClient.SqlConnection(connectionstring);        cmd = new System.Data.SqlClient.SqlCommand(cmdString, conn);        try        {            conn.Open();            cmd.ExecuteNonQuery();        }        //cmd.ExecuteNonQuery();        catch (Exception err)        {        }        finally        {            conn.Close();        }    }        void Application_End(object sender, EventArgs e)     {        //在应用程序关闭时运行的代码    }            void Application_Error(object sender, EventArgs e)     {         //在出现未处理的错误时运行的代码    }    void Session_Start(object sender, EventArgs e)     {        //在新会话启动时运行的代码    }    void Session_End(object sender, EventArgs e)     {        //下面的代码是关键,可解决IIS应用程序池自动回收的问题             System.Threading.Thread.Sleep(60 * 1000);        //触发事件, 写入提示信息            //这里设置你的web地址,可以随便指向你的任意一个aspx页面甚至不存在的页面,目的是要激发Application_Start              //使用您自己的URL             string url = "127.0.0.1";        System.Net.HttpWebRequest myHttpWebRequest = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(url);        System.Net.HttpWebResponse myHttpWebResponse = (System.Net.HttpWebResponse)myHttpWebRequest.GetResponse();        System.IO.Stream receiveStream = myHttpWebResponse.GetResponseStream();//得到回写的字节流               // 在会话结束时运行的代码。              // 注意: 只有在 Web.config 文件中的 sessionstate 模式设置为 InProc 时,才会引发 Session_End 事件。            // 如果会话模式设置为 StateServer      // 或 SQLServer,则不会引发该事件。     }       </script>