1

I am new to WatiN but made a few applications, and it's quite good. The problem is that my program is not responding while performing some web actions.

Here is my source code of WatiN:

using (var ie = new IE())
{
    StreamWriter wr = new StreamWriter(textBox1.Text+".txt");

    ie.GoTo("http://twitter.com/"+textBox1.Text+"/followers");

    string dm1 = ie.Body.Parent.OuterHtml;

    Match match1 = Regex.Match(dm1, "(?<=<strong>).*?(?=</strong> Followers)");
    string ck = match1.ToString();
    ck = ck.Replace(",", "");
    long check = Int64.Parse(ck);
    long n= 0;
    string pattern = "(?<=data-screen-name=\").*(?=data-name)";

    while (n <= check)
    {
        WatiN.Core.Settings.WaitUntilExistsTimeOut = 1;
        var focusme = ie.Div(Find.ByClass("stream-loading"));
        var element = focusme.NativeElement as IEElement;
        element.AsHtmlElement.scrollIntoView();

        string dm = ie.Body.Parent.OuterHtml;
        MatchCollection matches1 = Regex.Matches(dm, pattern);
        n = matches1.Count;
        label4.Text = n.ToString();
    }
    string dom = ie.Body.Parent.OuterHtml;

    MatchCollection matches = Regex.Matches(dom, pattern);
    foreach (Match match in matches)
    {
        string usr0 = match.ToString();
        int i = usr0.IndexOf("\"");
        string usr = usr0.Substring(0, i - 1);
        wr.WriteLine(usr);
    }
    label4.Text = label4.Text + " done";

    wr.Close();
}

This is the source, to get the twitter followers into a file. It's just a random example, but while performing this action, my program is not responding. I guess I have to create a new process for this action, but don't know exactly how to proceed.

EDIT: I am using this in the button1_Click, so basicly in the Form1 Class.

  • Before suggesting anything could you perform a step by step debug to see which instruction causes the program to hang? – Gabber Oct 10 '12 at 10:05

1 Answers1

1

There's tons of materials regarding such issues - it's not a problem with WatiN, but the design. You should move the WatiN core in a BackgroundWorker so the UI won't hang while executing the WatiN code.

WinForm Application UI Hangs during Long-Running Operation

Windows Forms Application - Slow/Unresponsive UI

And some guides:

http://www.codeproject.com/Articles/58292/Basic-Backgroundworker

http://www.dotnetperls.com/backgroundworker

Community
  • 1
  • 1
t3hn00b
  • 904
  • 5
  • 13