mjiansun 2016-04-07 11:41 采纳率: 50%
浏览 2751

C# 单步执行可以成功,直接运行却不能成功

# # 程序从timer2中执行到if (str1.Equals(""))

# {

# timer2.Enabled = false;

# textBox3.Text = "从联网状态变为断网,跳回到短间隔状态;";

# timer1.Enabled = true;

# timer1.Interval = 2000;

# }

# # 这段代码后,跳到timer1中,然后执行if条件成立时的代码,但就这段代码运行时会死机!

                private void timer1_Tick(object sender, EventArgs e)
        {
            TestNet testlink = new TestNet();
            string str1 = testlink.GetHostNameByIp("www.baidu.com");
            //Thread.Sleep(1000);
            if (str1.Equals(""))
            {
                timer1.Enabled = false;
                try
                {
                    this.webBrowser1 = new WebBrowser();
                    webBrowser1.ScriptErrorsSuppressed = true;
                    webBrowser1.Navigate("192.168.1.3");
                    //Thread.Sleep(1000); 
                    while (webBrowser1.ReadyState != System.Windows.Forms.WebBrowserReadyState.Complete)
                    {
                        //textBox3.Text = "加载页面中。。。";
                        System.Windows.Forms.Application.DoEvents();
                    }
                    HtmlElement btnSubmit = webBrowser1.Document.All["submit"];
                    HtmlElement tbUserid = webBrowser1.Document.All["username"];
                    HtmlElement tbPasswd = webBrowser1.Document.All["password"];
                    while (tbUserid == null || tbPasswd == null || btnSubmit == null)
                    {
                        btnSubmit = webBrowser1.Document.All["submit"];
                        tbUserid = webBrowser1.Document.All["username"];
                        tbPasswd = webBrowser1.Document.All["password"];
                    }
                    tbUserid.SetAttribute("value", _uname);
                    tbPasswd.SetAttribute("value", _pword);
                    btnSubmit.InvokeMember("click");
                    //MessageBox.Show("OK");
                    textBox3.Text = "没联网,在短间隔联网;";
                }
                catch (Exception ex)
                {
                    //webBrowser1.Navigate("192.168.1.3");
                    MessageBox.Show("重复没能访问到");
                }
                timer1.Enabled = true;
            }
            else
            {
                timer1.Enabled = false;
                textBox3.Text = "连上网了,变成长间隔联网;";
                timer2.Enabled = true;
                timer2.Interval = 5000;

            }
        }

                private void timer2_Tick(object sender, EventArgs e)
        {
            TestNet testlink = new TestNet();
            string str1 = testlink.GetHostNameByIp("www.baidu.com");
            if (str1.Equals(""))
            {
                timer2.Enabled = false;
                textBox3.Text = "从联网状态变为断网,跳回到短间隔状态;";
                timer1.Enabled = true;
                timer1.Interval = 2000;
            }
            else
            {
                timer2.Enabled = false;
                textBox3.Text = "一直在联网状态;";
                timer2.Enabled = true;
                timer2.Interval = 5000;
            }
        }
  • 写回答

1条回答

  • threenewbee 2016-04-07 21:00
    关注

    webBrowser1.ReadyState
    这个判断未必正确

    while (tbUserid == null || tbPasswd == null || btnSubmit == null)
    {
    这里也加上doevents
    }

    评论

报告相同问题?

悬赏问题

  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?
  • ¥100 求三轴之间相互配合画圆以及直线的算法
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发