1

私はWatiNを初めて使用しますが、いくつかのアプリケーションを作成しました。それは非常に優れています。問題は、いくつかのWebアクションの実行中にプログラムが応答しないことです。

これが私の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();
}

これは、Twitterフォロワーをファイルに入れるためのソースです。これは単なるランダムな例ですが、このアクションを実行している間、私のプログラムは応答していません。このアクションのために新しいプロセスを作成する必要があると思いますが、どのように進めるかは正確にはわかりません。

編集:私はbutton1_Clickでこれを使用しているので、基本的にForm1クラスで使用しています。

4

1 に答える 1

1

そのような問題に関してはたくさんの資料があります-それはWatiNの問題ではなく、デザインの問題です。WatiNコードの実行中にUIがハングしないように、BackgroundWorkerでWatiNコアを移動する必要があります。

長時間の操作中にWinFormアプリケーションUIがハングする

Windowsフォームアプリケーション-遅い/応答しないUI

そしていくつかのガイド:

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

http://www.dotnetperls.com/backgroundworker

于 2012-10-10T11:01:39.717 に答える