2011-07-27 57 views
2

我正在開發一個C#windows軟件。我想同時在那裏運行6個Web瀏覽器。在每個瀏覽器中有6個不同的網站導航,我想同時填充它們的值。 使用線程所有瀏覽器正在運行並且網站正確導航。但我的問題是,價值在每個瀏覽器中逐一填充。請建議我我如何同時填補所有值在每個站點,也後臺線程(在隱藏模式下,線程運行)C#的背景線程#

frmBrowser frmBz = new frmBrowser(); 

void bbyp_com() 
{ 
    frmBrowser1 frmB = new frmBrowser1(); 
    try 
    { 
     if (InvokeRequired) 
     { 
      this.Invoke(new MethodInvoker(bbyp_com)); 
      return; 
     } 
     frmB.MdiParent = frmBz; 
     frmB.Show(); 
     frmBz.LayoutMdi(MdiLayout.TileVertical);     

     if (!Navigate(ref frmB.webBrowser1,Global.Website[Global.WebsiteIndex(Global.ListingQueue[5])].urlListing)) 
      throw new System.ArgumentException("", ""); 

      frmB.Text = frmB.webBrowser1.Document.Url.ToString();         
      frmB.webBrowser1.Document.Forms["FrontPage_Form2"].GetElementsByTagName("input")["COMPANY"].SetAttribute("value", Global.XmlData.DocumentElement.SelectNodes("Author")[Global.AuthorDefault()].SelectSingleNode("ClientName").SelectSingleNode("ClientListing").SelectSingleNode("BusinessProfile").SelectSingleNode("BasicCompanyDetails").SelectSingleNode("BusinessTitle").InnerText); 
      frmB.webBrowser1.Document.Forms["FrontPage_Form2"].GetElementsByTagName("input")["CTYPE"].SetAttribute("value", Global.XmlData.DocumentElement.SelectNodes("Author")[Global.AuthorDefault()].SelectSingleNode("ClientName").SelectSingleNode("ClientListing").SelectSingleNode("BusinessProfile").SelectSingleNode("Category").SelectSingleNode("CategoryOption1").InnerText); 
      frmB.webBrowser1.Document.Forms["FrontPage_Form2"].GetElementsByTagName("input")["ADDRESS"].SetAttribute("value", Global.XmlData.DocumentElement.SelectNodes("Author")[Global.AuthorDefault()].SelectSingleNode("ClientName").SelectSingleNode("ClientListing").SelectSingleNode("BusinessProfile").SelectSingleNode("BasicCompanyDetails").SelectSingleNode("Street_Address").InnerText); 
      frmB.webBrowser1.Document.Forms["FrontPage_Form2"].GetElementsByTagName("input")["CITY"].SetAttribute("value", Global.XmlData.DocumentElement.SelectNodes("Author")[Global.AuthorDefault()].SelectSingleNode("ClientName").SelectSingleNode("ClientListing").SelectSingleNode("BusinessProfile").SelectSingleNode("BasicCompanyDetails").SelectSingleNode("City").InnerText); 

      if (!Global.ComboSelect(frmB.webBrowser1.Document.Forms["FrontPage_Form2"].GetElementsByTagName("select")["ST"], Global.XmlData.DocumentElement.SelectNodes("Author")[Global.AuthorDefault()].SelectSingleNode("ClientName").SelectSingleNode("ClientListing").SelectSingleNode("BusinessProfile").SelectSingleNode("BasicCompanyDetails").SelectSingleNode("State").InnerText, "Select a state")) 
       throw new System.ArgumentException("", ""); 

      frmB.webBrowser1.Document.Forms["FrontPage_Form2"].GetElementsByTagName("input")["ZIP"].SetAttribute("value", Global.XmlData.DocumentElement.SelectNodes("Author")[Global.AuthorDefault()].SelectSingleNode("ClientName").SelectSingleNode("ClientListing").SelectSingleNode("BusinessProfile").SelectSingleNode("BasicCompanyDetails").SelectSingleNode("Zipcode").InnerText); 
      frmB.webBrowser1.Document.Forms["FrontPage_Form2"].GetElementsByTagName("input")["BTN"].SetAttribute("value", Global.XmlData.DocumentElement.SelectNodes("Author")[Global.AuthorDefault()].SelectSingleNode("ClientName").SelectSingleNode("ClientListing").SelectSingleNode("BusinessProfile").SelectSingleNode("BasicCompanyDetails").SelectSingleNode("Phone").InnerText); 
      frmB.webBrowser1.Document.Forms["FrontPage_Form2"].GetElementsByTagName("input")["WEB_SITE"].SetAttribute("value", Global.XmlData.DocumentElement.SelectNodes("Author")[Global.AuthorDefault()].SelectSingleNode("ClientName").SelectSingleNode("ClientListing").SelectSingleNode("BusinessProfile").SelectSingleNode("BasicCompanyDetails").SelectSingleNode("Website").InnerText); 
      frmB.webBrowser1.Document.Forms["FrontPage_Form2"].GetElementsByTagName("input")["EMAIL"].SetAttribute("value", Global.XmlData.DocumentElement.SelectNodes("Author")[Global.AuthorDefault()].SelectSingleNode("ClientName").SelectSingleNode("ClientListing").SelectSingleNode("BusinessProfile").SelectSingleNode("BasicCompanyDetails").SelectSingleNode("ContactE-Mail").InnerText); 
      TimeDelay(1000); 

      frmB.webBrowser1.Document.Forms["FrontPage_Form2"].GetElementsByTagName("input")["B1"].InvokeMember("click"); 
      if (!IsDone(ref frmB.webBrowser1)) 
       throw new System.ArgumentException("", ""); 
      frmB.Text = frmB.webBrowser1.Document.Url.ToString(); 
      TimeDelay(50000); 
      if (!Success(ref frmB.webBrowser1, "Thank You!")) 
       throw new System.ArgumentException("", ""); 

      counter--; 
      TimeDelay(1000); 
      frmB.Close(); 

     } 
     catch (Exception) 
     { 
      counter--; 
      TimeDelay(1000); 
      frmB.Close(); 
      lblFailed.Text = (Convert.ToInt32(lblFailed.Text) + 1).ToString(); 
     } 
    } 

二功能

void yellowusa_com() 
    { 
     frmBrowser1 frmB = new frmBrowser1(); 
     try 
     {     
      if (InvokeRequired) 
      { 
       this.Invoke(new MethodInvoker(yellowusa_com)); 
       return; 
      } 
      frmB.MdiParent = frmBz; 
      frmB.Show(); 
      frmBz.LayoutMdi(MdiLayout.TileVertical); 

      if (!Navigate(ref frmB.webBrowser1,Global.Website[Global.WebsiteIndex(Global.ListingQueue[4])].urlListing)) 
       throw new System.ArgumentException("", ""); 
      frmB.Text = frmB.webBrowser1.Document.Url.ToString();       
      frmB.webBrowser1.Document.Forms["form"].GetElementsByTagName("input")["lname"].SetAttribute("value", Global.XmlData.DocumentElement.SelectNodes("Author")[Global.AuthorDefault()].SelectSingleNode("ClientName").SelectSingleNode("ClientListing").SelectSingleNode("BusinessProfile").SelectSingleNode("BasicCompanyDetails").SelectSingleNode("BusinessTitle").InnerText); 
      TimeDelay(100); 
      frmB.webBrowser1.Document.Forms["form"].GetElementsByTagName("input")["comments"].SetAttribute("value", Global.XmlData.DocumentElement.SelectNodes("Author")[Global.AuthorDefault()].SelectNodes("Website")[4].SelectSingleNode("Category").InnerText); 
      TimeDelay(200);    
      frmB.webBrowser1.Document.Forms["form"].GetElementsByTagName("input")["lContact"].SetAttribute("value", Global.XmlData.DocumentElement.SelectNodes("Author")[Global.AuthorDefault()].SelectSingleNode("ClientName").SelectSingleNode("ClientListing").SelectSingleNode("BusinessProfile").SelectSingleNode("ContactDetails").SelectSingleNode("Name").SelectSingleNode("FirstName").InnerText + ' ' 
                           + Global.XmlData.DocumentElement.SelectNodes("Author")[Global.AuthorDefault()].SelectSingleNode("ClientName").SelectSingleNode("ClientListing").SelectSingleNode("BusinessProfile").SelectSingleNode("ContactDetails").SelectSingleNode("Name").SelectSingleNode("LastName").InnerText); 
      frmB.webBrowser1.Document.Forms["form"].GetElementsByTagName("input")["lAddress"].SetAttribute("value", Global.XmlData.DocumentElement.SelectNodes("Author")[Global.AuthorDefault()].SelectSingleNode("ClientName").SelectSingleNode("ClientListing").SelectSingleNode("BusinessProfile").SelectSingleNode("BasicCompanyDetails").SelectSingleNode("Street_Address").InnerText); 

      frmB.webBrowser1.Document.Forms["form"].GetElementsByTagName("input")["lCity"].SetAttribute("value", Global.XmlData.DocumentElement.SelectNodes("Author")[Global.AuthorDefault()].SelectSingleNode("ClientName").SelectSingleNode("ClientListing").SelectSingleNode("BusinessProfile").SelectSingleNode("BasicCompanyDetails").SelectSingleNode("City").InnerText); 
      if (!Global.ComboSelect(frmB.webBrowser1.Document.Forms["form"].GetElementsByTagName("select")["lState"], Global.XmlData.DocumentElement.SelectNodes("Author")[Global.AuthorDefault()].SelectSingleNode("ClientName").SelectSingleNode("ClientListing").SelectSingleNode("BusinessProfile").SelectSingleNode("BasicCompanyDetails").SelectSingleNode("State").InnerText, "- Select")) 
       throw new System.ArgumentException("", ""); 
      string phone = Global.XmlData.DocumentElement.SelectNodes("Author")[Global.AuthorDefault()].SelectSingleNode("ClientName").SelectSingleNode("ClientListing").SelectSingleNode("BusinessProfile").SelectSingleNode("BasicCompanyDetails").SelectSingleNode("Phone").InnerText.Substring(0, 3) 
         + Global.XmlData.DocumentElement.SelectNodes("Author")[Global.AuthorDefault()].SelectSingleNode("ClientName").SelectSingleNode("ClientListing").SelectSingleNode("BusinessProfile").SelectSingleNode("BasicCompanyDetails").SelectSingleNode("Phone").InnerText.Substring(4, 3) 
         + Global.XmlData.DocumentElement.SelectNodes("Author")[Global.AuthorDefault()].SelectSingleNode("ClientName").SelectSingleNode("ClientListing").SelectSingleNode("BusinessProfile").SelectSingleNode("BasicCompanyDetails").SelectSingleNode("Phone").InnerText.Substring(8, 4); 

      frmB.webBrowser1.Document.Forms["form"].GetElementsByTagName("input")["lZip"].SetAttribute("value", Global.XmlData.DocumentElement.SelectNodes("Author")[Global.AuthorDefault()].SelectSingleNode("ClientName").SelectSingleNode("ClientListing").SelectSingleNode("BusinessProfile").SelectSingleNode("BasicCompanyDetails").SelectSingleNode("Zipcode").InnerText); 
      frmB.webBrowser1.Document.Forms["form"].GetElementsByTagName("input")["n1Phone"].SetAttribute("value", phone); 
      frmB.webBrowser1.Document.Forms["form"].GetElementsByTagName("input")["lEmail"].SetAttribute("value", Global.XmlData.DocumentElement.SelectNodes("Author")[Global.AuthorDefault()].SelectSingleNode("ClientName").SelectSingleNode("ClientListing").SelectSingleNode("BusinessProfile").SelectSingleNode("BasicCompanyDetails").SelectSingleNode("ContactE-Mail").InnerText); 


      try 
      { 
       string fax = Global.XmlData.DocumentElement.SelectNodes("Author")[Global.AuthorDefault()].SelectSingleNode("ClientName").SelectSingleNode("ClientListing").SelectSingleNode("BusinessProfile").SelectSingleNode("BasicCompanyDetails").SelectSingleNode("FaxNumber").InnerText.Substring(0, 3) 
          + Global.XmlData.DocumentElement.SelectNodes("Author")[Global.AuthorDefault()].SelectSingleNode("ClientName").SelectSingleNode("ClientListing").SelectSingleNode("BusinessProfile").SelectSingleNode("BasicCompanyDetails").SelectSingleNode("FaxNumber").InnerText.Substring(4, 3) 
          + Global.XmlData.DocumentElement.SelectNodes("Author")[Global.AuthorDefault()].SelectSingleNode("ClientName").SelectSingleNode("ClientListing").SelectSingleNode("BusinessProfile").SelectSingleNode("BasicCompanyDetails").SelectSingleNode("FaxNumber").InnerText.Substring(8, 4); 

       frmB.webBrowser1.Document.Forms["form"].GetElementsByTagName("input")["lFax"].SetAttribute("value",fax); 
      } 
      catch (Exception) 
      { } 

      frmB.webBrowser1.Document.Forms["form"].GetElementsByTagName("input")["websiteURL"].SetAttribute("value", Global.XmlData.DocumentElement.SelectNodes("Author")[Global.AuthorDefault()].SelectSingleNode("ClientName").SelectSingleNode("ClientListing").SelectSingleNode("BusinessProfile").SelectSingleNode("BasicCompanyDetails").SelectSingleNode("Website").InnerText); 
      frmB.webBrowser1.Document.Forms["form"].GetElementsByTagName("textarea")["describeBiz"].SetAttribute("value", Global.XmlData.DocumentElement.SelectNodes("Author")[Global.AuthorDefault()].SelectSingleNode("ClientName").SelectSingleNode("ClientListing").SelectSingleNode("BusinessProfile").SelectSingleNode("BasicCompanyDetails").SelectSingleNode("BusinessDescription").InnerText); 

      if (!ShowCaptcha(ref frmB.webBrowser1, "recaptcha")) 
       throw new System.ArgumentException("", ""); 

      string captchaval = objfrmCaptchaSettings.HumanCaptcha(); 

      frmB.webBrowser1.Document.Forms["form"].GetElementsByTagName("input")["recaptcha_response_field"].SetAttribute("value", captchaval); 
      frmB.webBrowser1.Document.Forms["form"].GetElementsByTagName("input")[17].InvokeMember("click"); 

      if (!IsDone(ref frmB.webBrowser1)) 
       throw new System.ArgumentException("", ""); 

      frmB.Text = frmB.webBrowser1.Document.Url.ToString(); 
      if (!Success(ref frmB.webBrowser1, "Thank You")) 
       throw new System.ArgumentException("", ""); 
      counter--; 
      TimeDelay(1000); 
      frmB.Close(); 

     } 
     catch (Exception) 
     { 
      counter--; 
      TimeDelay(1000); 
      frmB.Close(); 
      lblFailed.Text = (Convert.ToInt32(lblFailed.Text) + 1).ToString(); 
      Exception(); 
     } 
    } 

/*這裏frmBz是MDI父

這裏我只放了兩個功能,因爲空間不足

我正在使用這個

Thread m_thread1 = new Thread(new ThreadStart(hotfrog_com)); 
m_thread1.SetApartmentState(ApartmentState.STA); 

Thread m_thread2 = new Thread(new ThreadStart(yahoolocal_com)); 
m_thread2.SetApartmentState(ApartmentState.STA); 

Thread m_thread3 = new Thread(new ThreadStart(ezlocal_com)); 
m_thread3.SetApartmentState(ApartmentState.STA); 

Thread m_thread4 = new Thread(new ThreadStart(merchantcircle_com)); 
m_thread4.SetApartmentState(ApartmentState.STA); 

Thread m_thread5 = new Thread(new ThreadStart(yellowusa_com)); 
m_thread5.SetApartmentState(ApartmentState.STA); 

Thread m_thread6 = new Thread(new ThreadStart(bbyp_com)); 
m_thread6.SetApartmentState(ApartmentState.STA); 

要啓動主題 * selectedwebsites是列表 *

while (selectedwebsites.Count > 0) 
{ 
    try 
    { 
     if (selectedwebsites[j] == "hotfrog.com") 
     { 
      counter++; 
      m_thread1.Start(); 
      selectedwebsites.RemoveAt(j); 
      break; 
     } 

     if (selectedwebsites[j] == "local.yahoo.com") 
     { 
      counter++; 
      m_thread2.Start(); 
      selectedwebsites.RemoveAt(j); 
      break; 
     } 

     if (selectedwebsites[j] == "ezlocal.com") 
     { 
      counter++; 
      m_thread3.Start(); 
      selectedwebsites.RemoveAt(j); 
      break; 
     } 

     if (selectedwebsites[j] == "merchantcircle.com") 
     { 
      counter++; 
      m_thread4.Start(); 
      selectedwebsites.RemoveAt(j); 
      break; 
     } 

     if (selectedwebsites[j] == "yellowusa.com") 
     { 
      counter++; 
      m_thread5.Start(); 
      selectedwebsites.RemoveAt(j); 
      break; 
     } 

     if (selectedwebsites[j] == "bbyp.com") 
     { 
      counter++; 
      m_thread6.Start(); 
      selectedwebsites.RemoveAt(j); 
      break; 
     } 
    } 
    catch(Exception) {} 
} 
+0

您能否顯示您遇到問題的代碼? –

+0

Web瀏覽器是否以單一形式託管? –

+0

@Erno是的,我採取了單一的表單名稱(frmBrowser1),並有一個web瀏覽器。我還採取另一種形式(frmBrowser),它是MDI的父項。我在每個函數中創建了frmbrowser1的實例(代碼中顯示),並使其成爲frmBrowser形式的子項。請檢查代碼。 – Neetesh

回答

1

下面是裸露的基本知識,在自己的線程運行Method WebSiteOne。 我建議你閱讀關於線程,有很多很好的例子來開始。

Thread threadWebSiteOne = new Thread(new ThreadStart(WebSiteOne)); 
threadWebSiteOne.Start(); 

使用BackGroundWorker更新。

BackgroundWorker bw = new BackgroundWorker(); 
bw.DoWork += bw_DoWork; 
bw.RunWorkerCompleted += bw_RunWorkerCompleted; //Gets called when work completed 

bw.RunWorkerAsync(); 

static void bw_DoWork (object sender, DoWorkEventArgs e) { 
    //Load Webpage X EG : hotfrog_com(); 
} 
+0

這一個我用來創建6個不同thraeds – Neetesh

+0

線程m_thread1 = new線程(新的ThreadStart(hotfrog_com)); m_thread1.SetApartmentState(ApartmentState.STA); – Neetesh

+0

我認爲導航後我使用Application.doEvent(),所以它可能會逐一完成 – Neetesh

1

也許你已經添加一個後臺線程,然後打開瀏覽器一一在此線程?

嘗試System.Threading.Threadpool.QueueUserWorkItem()然後把你的電話打到瀏覽器中。

+0

好吧,我嘗試......... – Neetesh

+0

其導航所有6個網站6網絡瀏覽器,但它並沒有在所有瀏覽器中同時填寫細節。它一個接一個。我使用Application.DOEvent可能是這是原因 – Neetesh

2

你爲什麼用這個瀏覽器?你不能直接使用HTTP請求到服務器嗎?瀏覽器只提供一個用戶界面,只有在我認爲應該填寫「人」時才需要。

在你的情況下,如果你提交數據的網站會公開你作爲某種web服務填寫的表單(但這是最好的情況:))。

+0

我有大約100個網站,我想提交的數據使用軟件。所以我正在使用線程,並希望使用後臺線程 – Neetesh

+0

如果你有這麼多的網站(假設你自己維護它們),爲什麼你不公開一個web服務,所以你可以讓你的請求服務,而不是調用另一個處理您的請求。擁有服務將使您的代碼更容易閱讀和維護 – thekip

+0

不,我不維護網站,我只是自動化他們。 – Neetesh

1

問題是您的線程根本沒有任何有用的工作,因爲他們立即將所有相關的東西封送回UI線程。我在下面縮短了您的代碼,使其更易於查看。

void bbyp_com() 
{ 
    frmBrowser1 frmB = new frmBrowser1(); 
    try 
    { 
     if (InvokeRequired) 
     { 
      this.Invoke(new MethodInvoker(bbyp_com)); 
      return; 
     } 
     // All of the useful work happens here. 
    } 
    catch 
    { 
     // Stuff omitted here for brevity. 
    } 
} 

通知輔助線程立即電話如何Control.Invoke任何實際工作發生之前遞歸地重新輸入UI線程的方法。最重要的是,工作線程實際上等待UI線程在工作線程本身終止之前完成。從一開始,因爲創建新線程不過是浪費資源而已,從一開始就只是在UI線程上執行所有操作,效果更糟糕。

天真的解決方案是在工作線程中省略對Control.Invoke的調用並讓它完成所有工作。但是,唉,由於大多數有用的工作都需要對UI元素進行操作,所以這也不起作用,因爲您可能知道,UI元素除了UI線程之外都被禁止。你將不得不從根本上改變你的策略。

+0

其瀏覽所有6個網站6個瀏覽器,但其並未在所有瀏覽器中同時填寫詳細信息。它一個接一個。我使用Application.DOEvent可能是這個原因 – Neetesh