简体   繁体   中英

Application crashes on when run

Here is my code:

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");
        }
    }
}

But everytime I run it and fill in the textBox1 and textBox2 and submit them throu gh the application the whole window just stop working. Is there anything in my code that can make this problem or is there anything I can do to get rid of this problem? Also, I've never worked with c#, this is the first I've made and done in c# as I thought it would be fun to learn c#. Anyways the site that is going to be loaded uses a lot of javascript and i couldn't find a way to let the webBrowser1 use them, so I used webBrowser1.ScriptErrorsSuppressed = true; to ignore the error messages.

As far as I can see you are using:

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

And this will make your application unresponsive for 25 seconds each time you will execute button1_Click . It will happen because it's all handle by one thread. To avoid it try to do SendData() in separate thread.

Easy example would be:

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

You can try and do a read on threads here Run simple thread

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM