root/honeyclient/branches/exp/bhenderson-browser_automation/BrowserAutomation/Firefox.cs

Revision 1755, 2.0 kB (checked in by bhenderson, 5 months ago)

Reduced Processor hogging from hard timeout thread

  • Property svn:executable set to *
Line 
1 using System;
2 using System.Linq;
3 using System.Text;
4 using System.Threading;
5 using WatiN.Core.Mozilla;
6
7 namespace BrowserAutomation
8 {
9     class Firefox
10     {
11         private WatiN.Core.Mozilla.FireFox firefox;
12         private Thread hardTimeoutThread;
13         private Thread firefoxThread;
14         private String url;
15         private bool started;
16         private DateTime startTime;
17         private TimeSpan duration;
18
19         /// <summary>
20         /// Constructor for an instance of Firefox
21         /// </summary>
22         /// <param name="url">URL to be navigated to</param>
23         public Firefox(string url)
24         {
25             this.url = url;
26             started = false;
27
28             hardTimeoutThread = new Thread(new ThreadStart(HardTimeoutCallback));
29             firefoxThread = new Thread(new ThreadStart(FirefoxCallback));
30
31             firefoxThread.Start();
32             hardTimeoutThread.Start(); 
33         }
34
35         private void FirefoxCallback()
36         {
37             firefox = new WatiN.Core.Mozilla.FireFox();
38
39             try
40             {
41                 firefox.GoTo(url);
42                 Thread.Sleep(1000);
43                 firefox.Dispose();
44             }
45             catch (Exception e){}
46            
47             hardTimeoutThread.Abort();
48             firefoxThread.Abort();
49         }
50
51         /// <summary>
52         ///
53         /// </summary>
54         private void HardTimeoutCallback()
55         {
56             while (true)
57             {
58                 if (!started)
59                 {
60                     started = true;
61                     startTime = DateTime.Now;
62                 }
63
64                 Thread.Sleep(100);
65                 duration = DateTime.Now - startTime;
66
67                 if (duration.Seconds >= 30)
68                 {
69                     firefox.Dispose();
70                     firefoxThread.Abort();
71                     hardTimeoutThread.Abort();
72                 }
73             }
74         }
75     }
76 }
Note: See TracBrowser for help on using the browser.