1

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 through 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.

Tobbe
  • 1,825
  • 3
  • 21
  • 37
Patric Nøis
  • 208
  • 2
  • 8
  • 27
  • 1
    Does it stop working for 25 seconds by any chance? – Sayse Oct 02 '15 at 11:37
  • What is the exception that is being thrown? – Sievajet Oct 02 '15 at 11:38
  • @Sayse Yeah, that can be the problem, but when i used System.Threading.Thread.Sleep(10000); i wanted it to wait 10 seconds before doing the next thing – Patric Nøis Oct 02 '15 at 11:43
  • 2
    You may wish to read [When to use Task.Delay, when to use Thread.Sleep?](http://stackoverflow.com/questions/20082221/when-to-use-task-delay-when-to-use-thread-sleep) – Sayse Oct 02 '15 at 11:45

1 Answers1

1

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

Community
  • 1
  • 1
Mariusz
  • 31
  • 7
  • What i wanted when i used Thead.Sleep i wanted it to wait first 5 second after first .InvokeMember("click"); before doing the DoKrims(); but i cant see to get some timers for that too work – Patric Nøis Oct 02 '15 at 12:49