2 u013066730 u013066730 于 2016.04.07 19:41 提问

C# 单步执行可以成功,直接运行却不能成功
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个回答

caozhy
caozhy   Ds   Rxr 2016.04.08 05:00

webBrowser1.ReadyState
这个判断未必正确

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

u013066730
u013066730 为什么我在调试了非常多次后,还是会出现程序不执行的状态呢?
一年多之前 回复
u013066730
u013066730 另外我像问的是,为何说webBrowser1.ReadyState有可能判断不准确,这个判断不就是判断页面是否加载完全的吗?
一年多之前 回复
u013066730
u013066730 谢谢你的回答,我就是在while中加了句doevents,然后就成功了,这是为什么?
一年多之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!