我是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类中。
答案 0 :(得分:1)
关于这些问题的材料很多 - 它不是WatiN的问题,而是设计问题。您应该在BackgroundWorker中移动WatiN核心,以便在执行WatiN代码时UI不会挂起。
WinForm Application UI Hangs during Long-Running Operation
Windows Forms Application - Slow/Unresponsive UI
还有一些指南:
http://www.codeproject.com/Articles/58292/Basic-Backgroundworker