.NET 延时执行、定时执行

网上的定时器.全是 js 的呢? 中国的一大特色..拷贝..其实.net有自己的方法实现.

public partial class wall_w_download : System.Web.UI.Page

{

    System.Timers.Timer myTimer;

    protected void Page_Load(object sender, EventArgs e)

    {



                    string getUrl = "http://www.baidu.com“;

                    //定时器,间隔N秒后执行赠送.

                    myTimer = new System.Timers.Timer(1000*60*3);

                    myTimer.Enabled = true;

                    myTimer.Elapsed += delegate(object source, System.Timers.ElapsedEventArgs ex)

                    {

                        GetGeneralContent(getUrl);

                        myTimer.Stop();

                        myTimer.Close();

                        myTimer.Dispose();

                    };

               

    }





    private string GetGeneralContent(string strUrl)

    {

        string strMsg = string.Empty;

        try

        {

            WebRequest request = WebRequest.Create(strUrl);

            WebResponse response = request.GetResponse();

            StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding("gb2312"));



            strMsg = reader.ReadToEnd();



            reader.Close();

            reader.Dispose();

            response.Close();

        }

        catch(Exception xe)

        {

            strMsg = xe.ToString();

        }

        return strMsg;

    }

}

 

 

你可能感兴趣的:(.net)