我正在尝试使用计时器填充网页中的某些TextBox,但是在填充第一个TextBox之后,计时器停止并且该应用程序将无法执行任何操作。 我用一个按钮启动计时器。
此代码:
private void startBrute()
{
foreach (HtmlElement user in webBrowser1.Document.All.GetElementsByName("username"))
{
user.InnerText = "user1";
}
foreach (HtmlElement pass in webBrowser1.Document.All.GetElementsByName("password"))
{
pass.InnerText = "123456";
}
foreach (HtmlElement search in webBrowser1.Document.GetElementsByTagName("button"))
{
if (search.InnerText == "Log in")
{
search.InvokeMember("click");
}
}
timer2.Stop();
}
我在计时器tick(timer2)中调用Brute方法
private void timer2_Tick_1(object sender, EventArgs e)
{
startBrute();
}
然后我在按钮中启动计时器:
private void btnStart_Click(object sender, EventArgs e)
{
timer2.Start();
}
答案 0 :(得分:-2)
也许您的计时器将在ASP Page之后停下来,所以您必须从以下名称空间声明您的计时器对象
System.Web.UI.Timer
答案 1 :(得分:-5)
尝试调用javascript ajax并让控制器休眠:
//查看JAVASCRIPT:
function callSleep() {
var dataobject = { sleepSec: 1 }
$.ajax({
aysnc: true,
url: '@Url.Action("sleepSeconds", "JobSetup")',
type: "POST",
data: dataobject,
success: function (results) {
// when done, sleepSeconds() returns here
}
});
}
//控制器:
[HttpPost, ValidateInput(false)]
public ActionResult sleepSeconds(double sleepSec)
{
int secs = (int)(sleepSec * 1000.00);
System.Threading.Thread.Sleep(secs);
return Json("{\"SLEEP\":\"" + sleepSec + "\"}",JsonRequestBehavior.AllowGet);
}