2013-04-06 69 views
1

我想用5個虛擬(隱藏)Gecko(Xulrunner)瀏覽器做一個應用程序。但是當我嘗試在Threading中創建一個瀏覽器時,它在GeckoPreferences中返回錯誤,我完全同意它!壁虎多線程錯誤

這裏代碼樣品:

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Text; 
using System.Windows.Forms; 
using DevExpress.XtraEditors; 
using Skybound.Gecko; 
using System.Threading; 
namespace Gekco_Test 
{ 
public partial class Main : DevExpress.XtraEditors.XtraForm 
{ 
    public Main() 
    { 
     InitializeComponent(); 
     CheckForIllegalCrossThreadCalls = false; 
    } 

    private void Main_Load(object sender, EventArgs e) 
    { 

    } 

    private void simpleButton1_Click(object sender, EventArgs e) 
    { 
     Thread th = new Thread(webControllerFunc); 
     th.SetApartmentState(ApartmentState.STA); 
     th.Start(); 


    } 
    void webControllerFunc() 
    { 
     geckoWebControl gControll = new geckoWebControl(); 
     gControll.webBrowserAccess("91.213.108.178", 80); 
    } 

} 

class geckoWebControl 
{ 
    bool readyState; 
    GeckoWebBrowser wb = new GeckoWebBrowser(); 
    public string webBrowserAccess(string host,int port) 
    { 
     Skybound.Gecko.Xpcom.Initialize(Application.StartupPath + "\\xulrunner\\"); 
     readyState = false; 
     Form form = new Form(); 
     GeckoPreferences.User["network.proxy.http"] = host; 
     GeckoPreferences.User["network.proxy.http_port"] = port; 
     GeckoPreferences.User["network.proxy.type"] = 1; 
     wb.Navigate("about:blank"); 
     wb.DocumentCompleted += wb_DocumentCompleted; 

     while (!readyState) 
      Application.DoEvents(); 

     return wb.Document.TextContent; 
    } 

    void wb_DocumentCompleted(object sender, EventArgs e) 
    { 
     readyState = true; 
    } 

} 

}

錯誤:

{「無法轉換類型的COM對象 '系統.__ ComObject' 到接口類型 'Skybound.Gecko.nsIServiceManager' 。此操作失敗,因爲IIC'{8BB35ED9-E332-462D-9155-4A002AB5C958}'接口的COM組件上的QueryInterface調用由於以下錯誤而失敗:沒有支持此接口(異常來自HRESULT:0x80004002(E_NOINTERFACE) )。「}

謝謝!

回答

2

壁虎不支持多線程。所以你可以在代碼中使用它來在線程中使用它。

this.BeginInvoke(new Action(() => { 
//What you want gecko browser to do! Like: 
geckoBrowser.navigate("http://somewhere.com"); 
}));