应用程序在运行时崩溃

时间:2015-10-02 11:32:12

标签: javascript c# crash hang

这是我的代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace MafiaspilletBot
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            webBrowser1.Navigate("http://mafiaspillet.no/");
            webBrowser1.ScriptErrorsSuppressed = true;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            SendData();
        }
        private void SendData()
        {
            webBrowser1.Document.GetElementsByTagName("input").GetElementsByName("brukernavn")[0].SetAttribute("value", textBox2.Text);
            webBrowser1.Document.GetElementsByTagName("input").GetElementsByName("passord")[0].SetAttribute("value", textBox1.Text);
            System.Threading.Thread.Sleep(5000);
            webBrowser1.Document.GetElementsByTagName("input").GetElementsByName("login_buton")[0].InvokeMember("click");
            System.Threading.Thread.Sleep(10000);
            DoKrims();
        }
        private void DoKrims()
        {
            webBrowser1.Navigate("http://mafiaspillet.no/kriminalitet3.php");
            System.Threading.Thread.Sleep(10000);
            webBrowser1.Document.GetElementById("submit").InvokeMember("click");
        }
    }
}

但每次我运行它并填写textBox1textBox2 and submit them throu gh应用程序时,整个窗口就会停止工作。我的代码中是否有任何可以解决此问题的内容,或者我可以做些什么来摆脱这个问题?此外,我从未使用过c#,这是我用c#制作和完成的第一个,因为我认为学习c#会很有趣。无论如何,要加载的网站使用了大量的javascript而我无法找到让webBrowser1使用它们的方法,因此我使用webBrowser1.ScriptErrorsSuppressed = true;来忽略错误消息。< / p>

1 个答案:

答案 0 :(得分:1)

据我所知,你正在使用:

System.Threading.Thread.Sleep(5000);
System.Threading.Thread.Sleep(10000);
System.Threading.Thread.Sleep(10000);

每次执行button1_Click时,这将使您的应用程序无响应25秒。它会发生,因为它全部由一个线程处理。为避免它,请尝试在单独的线程中执行SendData()

简单的例子是:

Thread SendThread = new Thread(SendData);
SendThread.Start();

您可以尝试在此处阅读Run simple thread

相关问题