Happy-Ferret / geckofx

Automatically exported from code.google.com/p/geckofx
0 stars 0 forks source link

Not able to Navigate the geko browser in a single threaded apartment #5

Open GoogleCodeExporter opened 8 years ago

GoogleCodeExporter commented 8 years ago
Hello,
I am not able to navigate the geko  browser in a single threaded 
apartment...and the error I am getting is...

{"Cannot call Navigate() before the window handle is created."}

Please Guide....

Regards,
bhavin.chheda@vizualize.com.

Original issue reported on code.google.com by bmchhe...@gmail.com on 11 Aug 2008 at 1:20

GoogleCodeExporter commented 8 years ago
You should set the parent of the GeckoWebBrowser object to another control 
which 
"contains" it.  E.g.

            GeckoWebBrowser wb = new GeckoWebBrowser();
            wb.Parent = panel1;

Call Navigate() after this and it will work.

Original comment by surya.sr...@gmail.com on 13 Mar 2009 at 12:07

GoogleCodeExporter commented 8 years ago
This solution does not work if the GeckoWebBrowser is not actually drawn on 
screen.

Original comment by alen.siljak on 26 Jun 2009 at 7:26

GoogleCodeExporter commented 8 years ago
Is there anywhere a sample that shows how to get a browser running and 
displaying a
page without this error? I'm putting it in a panel in a single-threaded 
application,
I would *think* that GeckoWebBrowser is being "drawn on the screen", but I'm 
getting
this error anyway.

I don't know what I'm doing wrong, that's why a sample from A-Z on how to put 
it on a
form and display a web page would be handy.

Original comment by helgi.bi...@gmail.com on 2 Feb 2010 at 8:01

GoogleCodeExporter commented 8 years ago
Hi,

I am facing the same problem.
I noticed that it happens only when the browser not drawn on screen.
In my application, I have a windows forms tab control and the browser is in one 
of the tab pages.
So my solution (which is not user friendly) is to switch to that tab before 
navigating with the browser control.
I think the problem is that the actual handle to the control is not created 
until the browser control is shown.
If you solved it using another better technique, please share.

Original comment by jacob.d...@gmail.com on 13 Jun 2010 at 6:18

GoogleCodeExporter commented 8 years ago
This is my workaround:

if (this.tabControl1.SelectedTab != this.tpFFBrowser)
{
    this.tabControl1.SelectedTab = this.tpFFBrowser;
    Application.DoEvents();
}
this.firefoxBrowser.Navigate(this.txtURL.Text);

Original comment by jacob.d...@gmail.com on 13 Jun 2010 at 6:24

GoogleCodeExporter commented 8 years ago
I had a very similar problem which I got around by adding these lines:

//Constructor for the form
public form1()
{
     InitializeComponent();

     geckoBrowser1.HandleCreated += new EventHandler(geckoBrowser1_HandleCreated);
}

//Handle Created EventHandler
private void geckoBrowser1_HandleCreated(object sender, EventArgs e)
{
     geckoBrowser1.Navigate("www.google.com");
}

In this way the system waits until the handle is created for the navigate.  I 
dont know how much help this will be but this is what I used to cause my system 
to start on a specific page and it seems to work.

Original comment by One2F...@gmail.com on 17 Jun 2010 at 8:06

GoogleCodeExporter commented 8 years ago
I fixed this error by adding the following code in the Navigate Method of the 
GeckoWebBrowser Class in GeckoWebBrowser.cs

public bool Navigate(string url, GeckoLoadFlags loadFlags, string referrer, 
byte [] postData, string additionalHeaders)
        {
            star:
            if (string.IsNullOrEmpty(url))
                return false;

            if (IsHandleCreated)
            {
                if (IsBusy)
                    this.Stop();

                // WebNav.LoadURI throws an exception if we try to open a file that doesn't exist...
                Uri created;
                if (Uri.TryCreate(url, UriKind.RelativeOrAbsolute, out created) && created.IsAbsoluteUri && created.IsFile)
                {
                    if (!File.Exists(created.LocalPath) && !Directory.Exists(created.LocalPath))
                        return false;
                }

                nsIInputStream postDataStream = null, headersStream = null;

                if (postData != null)
                {
                    // post data must start with CRLF.  actually, you can put additional headers before this, but there's no
                    // point because this method has an "additionalHeaders" argument.  so we might as well insert it automatically
                    Array.Resize(ref postData, postData.Length + 2);
                    Array.Copy(postData, 0, postData, 2, postData.Length - 2);
                    postData[0] = (byte)'\r';
                    postData[1] = (byte)'\n';
                    postDataStream = ByteArrayInputStream.Create(postData);
                }

                if (!string.IsNullOrEmpty(additionalHeaders))
                {
                    // each header must end with a CRLF (including the last one)
                    // here we simply ensure that the last header has a CRLF.  if the header has other syntax problems,
                    // they're the caller's responsibility
                    if (!additionalHeaders.EndsWith("\r\n"))
                        additionalHeaders += "\r\n";

                    headersStream = ByteArrayInputStream.Create(Encoding.UTF8.GetBytes(additionalHeaders));
                }

                nsIURI referrerUri = null;
                if (!string.IsNullOrEmpty(referrer))
                {
                    referrerUri = Xpcom.GetService<nsIIOService>("@mozilla.org/network/io-service;1").NewURI(new nsACString(referrer), null, null);
                }

                return (WebNav.LoadURI(url, (uint)loadFlags, referrerUri, postDataStream, headersStream) != 0);
            }
            else
            {
                CreateHandle();
                goto star;
                //throw new InvalidOperationException("Cannot call Navigate() before the window handle is created.");
            }
        }

Hope it helps.

Even Though I know Using Labels are not recommended but this is a quick fix.

Original comment by initiote...@gmail.com on 10 Apr 2011 at 2:47

GoogleCodeExporter commented 8 years ago
This behavior has not changed in geckofx 1.9.1.0. I modified Navigate to call 
CreateHandle() if IsHandleCreated is null. Works perfect.

There are a few tricks to building geckofx.
Start here: http://www.webprogrammingblog.com/compile-geckofx/

GeckoWebWrowser.cs: Line 420:

public bool Navigate(string url, GeckoLoadFlags loadFlags, string referrer, 
byte [] postData, string additionalHeaders)
{
    if (string.IsNullOrEmpty(url)) return false;

    if (!IsHandleCreated) CreateHandle(); // Add this and removed if and else for check

    if (IsBusy) this.Stop();

    Uri created;
    if (Uri.TryCreate(url, UriKind.RelativeOrAbsolute, out created) && created.IsAbsoluteUri && created.IsFile)
    {
        if (!File.Exists(created.LocalPath) && !Directory.Exists(created.LocalPath))
            return false;
    }

    nsIInputStream postDataStream = null, headersStream = null;

    if (postData != null)
    {
        Array.Resize(ref postData, postData.Length + 2);
        Array.Copy(postData, 0, postData, 2, postData.Length - 2);
        postData[0] = (byte)'\r';
        postData[1] = (byte)'\n';
        postDataStream = ByteArrayInputStream.Create(postData);
    }

    if (!string.IsNullOrEmpty(additionalHeaders))
    {
        if (!additionalHeaders.EndsWith("\r\n")) additionalHeaders += "\r\n";

        headersStream = ByteArrayInputStream.Create(Encoding.UTF8.GetBytes(additionalHeaders));
    }

    nsIURI referrerUri = null;
    if (!string.IsNullOrEmpty(referrer))
    {
        referrerUri = Xpcom.GetService<nsIIOService>("@mozilla.org/network/io-service;1").NewURI(new nsACString(referrer), null, null);
    }

    return (WebNav.LoadURI(url, (uint)loadFlags, referrerUri, postDataStream, headersStream) != 0);

}

Original comment by jared.de...@gmail.com on 7 Jul 2012 at 6:44