2014-09-19 27 views
0

我写,能实现我们的人工网页表单输入的一个应用程序处理C#应用程序,蜱活动和等待

一切除了一个问题是工作好。

我有一个定时器设置,在某个页面上启用。计时器滴答事件正在每100毫秒检查页面上的Ajax更改。一旦检测到ajax更新,定时器被禁用,结果被存储,并且程序应该继续执行超出该点的代码。

问题是代码在定时器启用时继续执行。

在逻辑,只要适当页面加载,我有

t2.Enabled = TRUE;

这将立即生效,因为它应该查看的页面更新之前,发现

但发现结果之前,以下Enabled属性立即执行代码的无停顿,造成了许多问题,如变量变更。

怎样才可以有下面这行等待的代码,直到t2.Enabled被设置为false(这是

void t2_Tick(object sender, EventArgs e) 
    { 
     string postVerifyHTML = string.Empty; 

     try 
     { 
      postVerifyHTML = wb.Document.Body.InnerHtml; 
     } 
     // if page fails, restart 
     catch 
     { 
      wb.Navigate(new Uri("http://www.website.com"), "_self"); 
     } 

     if (postVerifyHTML.IndexOf("indentifier html") != -1) 
     { 
      NameSearchResults[nameCounter].Visited = true; 
      nameCounter++; 
      ResultFound = true; 
      t2.Enabled = false; 
     } 

     t2TimerCount++; 

     if (t2TimerCount >= 100) 
     { 
      // TRY AGAIN 
      wb.Navigate(new Uri("http://www.website.com"), "_self"); 
     } 

    } 



    protected void wb_SearchForm_DocumentCompleted(object sender, EventArgs e) 
    { 
     string pageHTML = wb.Document.Body.InnerHtml; 

     // Look at the page with the name result 
     if (pageHTML.IndexOf("Search Results: Verify") != -1) 
     { 
      //If the page has this input, a verification is available 
      if (pageHTML.IndexOf("txtSSN") != -1) 
      { 
       HtmlElement txtSSN = wb.Document.GetElementById("txtSSN"); 
       txtSSN.SetAttribute("value", curSearchRecord.UniqueId.Replace("-", "").Replace(" ", "")); 
       HtmlElement submitBtn = wb.Document.GetElementById("ibtnVerify"); 
       submitBtn.InvokeMember("click"); 

       t2.Enabled = true; 
       // I need the code after this point to wait until the Timer is disabled 

    } 

回答

0

定时器运行的t2_Tick(对象发件人,EventArgs的)方法中完成在你的UI代码的不同线程上,这就是为什么你的执行仍在继续为什么你不简单地检查Timer的Enabled状态来决定是否继续执行?或者使用你的ajax代码的回调函数来触发关闭后续代码

+0

我试图把 而(t2.Enabled) { }在该行后面,但只是让应用程序停留在该循环中 – mrb398 2014-09-19 12:56:54

+0

您应该可能会发布代码,以便清楚其余代码所在的位置,但是while循环并非真正合适 - 您需要使用如果应该在每次Timer tick事件触发时进行测试。 – 2014-09-19 12:59:55

+0

我刚刚在相关的代码中加入了我的帖子 – mrb398 2014-09-19 13:10:40

0

林不知道这是最好的方法,但你可以做一个如果像这样:

if (t2.Enabled=False) 
    { 
     //the code you want to run when the timer is off 
    } 

但你必须确保它是在另一个定时器(T3在这种情况下,如果你想),否则它不会检查每个打勾,如T2是关闭运行的代码,同时它。 对不起,如果答案不是更详细的,我也缺乏你的问题的细节。

很好的编程:)

+0

感谢您的回复。不过,我想我找到了一个不同的解决方案,我将post-Timer代码移动到一个单独的方法中,并在t2_Tick中的适当时间调用该方法。到目前为止,这似乎是一个坚实的修复:-) – mrb398 2014-09-19 13:27:18

+0

很好:)很高兴youre问题解决:) – cyberced211 2014-09-19 13:30:16

0

你可以尝试使用ManualResetEvent作为类的成员

后启用定时器,调用了WaitOne方法

您禁止定时器后,调用设置方法

private ManualResetevent mre = new ManualResetEvent(false); 

void t2_Tick(object sender, EventArgs e) 
{ 
    string postVerifyHTML = string.Empty; 

    try 
    { 
     postVerifyHTML = wb.Document.Body.InnerHtml; 
    } 
    // if page fails, restart 
    catch 
    { 
     wb.Navigate(new Uri("http://www.website.com"), "_self"); 
    } 

    if (postVerifyHTML.IndexOf("indentifier html") != -1) 
    { 
     NameSearchResults[nameCounter].Visited = true; 
     nameCounter++; 
     ResultFound = true; 
     t2.Enabled = false; 

     //Set the mre to unblock the blocked code 
     mre.Set(); 
    } 

    t2TimerCount++; 

    if (t2TimerCount >= 100) 
    { 
     // TRY AGAIN 
     wb.Navigate(new Uri("http://www.website.com"), "_self"); 
    } 

} 

protected void wb_SearchForm_DocumentCompleted(object sender, EventArgs e) 
{ 
    string pageHTML = wb.Document.Body.InnerHtml; 

    // Look at the page with the name result 
    if (pageHTML.IndexOf("Search Results: Verify") != -1) 
    { 
     //If the page has this input, a verification is available 
     if (pageHTML.IndexOf("txtSSN") != -1) 
     { 
      HtmlElement txtSSN = wb.Document.GetElementById("txtSSN"); 
      txtSSN.SetAttribute("value", curSearchRecord.UniqueId.Replace("-", "").Replace(" ", "")); 
      HtmlElement submitBtn = wb.Document.GetElementById("ibtnVerify"); 
      submitBtn.InvokeMember("click"); 

      t2.Enabled = true; 

      //The code will block until Set() is called on mre 
      mre.WaitOne(); 

      //The rest of your code here 

}