如何在asp.net的cs代码页面里面(不用js)写一个计时器

时间:2021-10-26 04:55:22
比如设置一个计时器,开始计算时间,并执行某个方法,如果10秒后该方法还未执行完毕,我要另外处理。
请问这个计时器如何来写?

7 个解决方案

#1


用AJAX中的 updatepanel和 线程

或者时间控件

#2


  private bool bOk = false;
        protected void Page_Load(object sender, EventArgs e)
        {


            System.Threading.Thread t = new Thread(new System.Threading.ThreadStart(this.DoSomething));
            t.Start();
            int i = 0;
            while (true)
            {
                i++;
                if (bOk)
                {
                    Response.Write("OK!");
                    break;
                }
                else if (i > 3)
                {
                    Response.Write("超时!");
                    break;
                }

                System.Threading.Thread.Sleep(1000);   
            }

        }

        void DoSomething()
        {
            System.Threading.Thread.Sleep(5000);
            bOk = true;
        }


不超时的情况,你将5000改为1000

#3


顶楼上

#4


学习了

#5


简单的可以使用Timer控件。

#6


顶一下

#7


        public void GetImg()
        {
            
            HttpWebRequest req = (HttpWebRequest)WebRequest.Create(URL);
            req.AllowAutoRedirect = true;
            req.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0; SLCC1; .NET CLR 2.0.50727; .NET CLR 3.0.04506; .NET CLR 3.5.21022; .NET CLR 1.0.3705; .NET CLR 1.1.4322)";
            // req.Referer = "http://www.cognifide.com";//同上面的注释 
            req.ContentType = "text/html";
            req.AllowWriteStreamBuffering = true;
            req.AutomaticDecompression = DecompressionMethods.GZip;
            req.Method = "GET";
            req.Proxy = null;
            req.ReadWriteTimeout = 20;



            HttpStatusCode status;
            using (HttpWebResponse resp = (HttpWebResponse)req.GetResponse())
            {
                status = resp.StatusCode;
            }


            if (status == HttpStatusCode.OK || status == HttpStatusCode.Moved)
            {

                //Doit();



                System.Threading.Thread t = new Thread(new System.Threading.ThreadStart(this.Doit));
                t.Start();



                int i = 0;
                while (true)
                {

                    i++;



                    if (this.bOk)
                    {
                        //Response.Write("OK!");

                        break;
                    }
                    else if (i > 10)
                    {
                        //Response.Write("超时!");

                        break;
                    }

                    System.Threading.Thread.Sleep(1000);
                }

                t.Abort();

                //return true;
            }
            else
            {
                //return false;
            }
            
        }

        void Doit()
        {
            webBrowser.Navigate(URL);

            while (webBrowser.ReadyState != WebBrowserReadyState.Complete)
            {
                
                Application.DoEvents();

            }

            bOk = true;
        }

我的代码是实现在线截图的功能,一般的网站都能解图,但是遇到那种一直不停加载又加载不完的网站(http://www.upschool.com.cn/edu/1317/2006/417/10du252414_1.shtml),就不行了。所以想用计数器来实现,超过10秒,就我另作处理。现在用了2楼的代码,但是发现在webBrowser.Navigate(URL);之后用MessageBox.Show("提示")就不相应了,但是在webBrowser.Navigate(URL);之前用MessageBox.Show("提示")就能响应的。

#1


用AJAX中的 updatepanel和 线程

或者时间控件

#2


  private bool bOk = false;
        protected void Page_Load(object sender, EventArgs e)
        {


            System.Threading.Thread t = new Thread(new System.Threading.ThreadStart(this.DoSomething));
            t.Start();
            int i = 0;
            while (true)
            {
                i++;
                if (bOk)
                {
                    Response.Write("OK!");
                    break;
                }
                else if (i > 3)
                {
                    Response.Write("超时!");
                    break;
                }

                System.Threading.Thread.Sleep(1000);   
            }

        }

        void DoSomething()
        {
            System.Threading.Thread.Sleep(5000);
            bOk = true;
        }


不超时的情况,你将5000改为1000

#3


顶楼上

#4


学习了

#5


简单的可以使用Timer控件。

#6


顶一下

#7


        public void GetImg()
        {
            
            HttpWebRequest req = (HttpWebRequest)WebRequest.Create(URL);
            req.AllowAutoRedirect = true;
            req.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0; SLCC1; .NET CLR 2.0.50727; .NET CLR 3.0.04506; .NET CLR 3.5.21022; .NET CLR 1.0.3705; .NET CLR 1.1.4322)";
            // req.Referer = "http://www.cognifide.com";//同上面的注释 
            req.ContentType = "text/html";
            req.AllowWriteStreamBuffering = true;
            req.AutomaticDecompression = DecompressionMethods.GZip;
            req.Method = "GET";
            req.Proxy = null;
            req.ReadWriteTimeout = 20;



            HttpStatusCode status;
            using (HttpWebResponse resp = (HttpWebResponse)req.GetResponse())
            {
                status = resp.StatusCode;
            }


            if (status == HttpStatusCode.OK || status == HttpStatusCode.Moved)
            {

                //Doit();



                System.Threading.Thread t = new Thread(new System.Threading.ThreadStart(this.Doit));
                t.Start();



                int i = 0;
                while (true)
                {

                    i++;



                    if (this.bOk)
                    {
                        //Response.Write("OK!");

                        break;
                    }
                    else if (i > 10)
                    {
                        //Response.Write("超时!");

                        break;
                    }

                    System.Threading.Thread.Sleep(1000);
                }

                t.Abort();

                //return true;
            }
            else
            {
                //return false;
            }
            
        }

        void Doit()
        {
            webBrowser.Navigate(URL);

            while (webBrowser.ReadyState != WebBrowserReadyState.Complete)
            {
                
                Application.DoEvents();

            }

            bOk = true;
        }

我的代码是实现在线截图的功能,一般的网站都能解图,但是遇到那种一直不停加载又加载不完的网站(http://www.upschool.com.cn/edu/1317/2006/417/10du252414_1.shtml),就不行了。所以想用计数器来实现,超过10秒,就我另作处理。现在用了2楼的代码,但是发现在webBrowser.Navigate(URL);之后用MessageBox.Show("提示")就不相应了,但是在webBrowser.Navigate(URL);之前用MessageBox.Show("提示")就能响应的。