tags:

views:

175

answers:

3

I am using WATin IE component for browsing a specific website On StartBrowsing button click event I am initializing the object of WatiN.Core.IE and passing the website URL for opening the website as shown in the code snippet below :-

WatiN.Core.IE ie;  

private void btnStartBrowsing_Click(object sender, EventArgs e)
{          
  ie = new IE(URLs.mainURL);
}

Sometimes or rather i should say 5/10 times I am getting this error :- "Timeout while Internet Explorer busy"

How to get around this problem??

+2  A: 

It is possible that the IE instance isn't being disposed properly. You'll want to check Task Manager and see how many open IEXPLORE.EXE processes you have running. If you have more than two, I would suggest implementing a using block and then checking Task Manager again:

using (IE ie = new IE(URLs.mainURL)
{
    ...
}

An even better solution would be to use the AttachTo method once you have initialized a "master" browser instance variable:

private IE ie = new IE();

public void btnStartBrowsing_Click(object sender, EventArgs e)
{
    using ie2 = ie.AttachTo<IE>(Find.ByUrl(URLs.mainURL))
    {
        ...
    }
}

UPDATE: Since you need access to the browser for the duration of the session, I would strongly suggest using a singleton object to house the browser object:

public sealed class BrowserIE
{
    static readonly IE _Instance = new IE();

    static BrowserIE()
    {
    }

    BrowserIE()
    {
    }

    public static IE Instance
    {
        get { return _Instance; }
    }
}

The browser is only opened one time, and you have access to it whenever you need it, since you don't have to manually instantiate it. To use it, you simply call the Instance property every time you need access to the browser:

BrowserIE.Instance.GoTo(URLs.mainURL);
Divs headerDivs = BrowserIE.Instance.Divs.Filter(Find.ByClass("header"));
Neil T.
Sorry for so late reply Neil T but I was considering various architectures to implement using but still I was getting th error.......moreover I need to use the instance for the whole duration of application running time...............................and guys please help me in this question also http://stackoverflow.com/questions/3766784/problem-in-updating-datagridview-via-a-thread-when-scrolling
Ankush Roy
A: 

It might be a race condition try to increase the watin timeouts before you initialize the ie object.

Like this:

   Settings.AttachToBrowserTimeOut = 240;
   Settings.WaitUntilExistsTimeOut = 240;
   Settings.WaitForCompleteTimeOut = 240;             
alonp
+1  A: 

Hi, I use this class to attach to browser. And it works fine.

public class StaticIE : IDisposable
{
    private IE _ie;
    private int _ieThread;
    private string _ieHwnd;
    private int _processId;

    public StaticIE(IE aBrowser)
    {
        SetBrowser(aBrowser);
    }

    public IE Browser
    {
        get
        {
            var currentThreadId = GetCurrentThreadId();
            _ie = Browser.AttachTo<IE>(Find.By("hwnd", _ieHwnd), 1);
            if (currentThreadId != _ieThread)
            {
                _ieThread = currentThreadId;
            }
            return _ie;
        }
    }

    private void SetBrowser(IE aBrowser)
    {
        _ie = aBrowser;
        _ieHwnd = _ie.hWnd.ToString();
        _ieThread = GetCurrentThreadId();
        _processId = _ie.ProcessID;
    }

    private int GetCurrentThreadId()
    {
        return Thread.CurrentThread.GetHashCode();
    }

    public void Dispose()
    {
        if (_ie != null)
        {
            Process.GetProcessById(_processId).Kill();
            _ie = null;
        }
    }
}
Pavlo Neyman