2011-07-27 6 views
2

私はソフトウェアC#の窓に取り組んでいます。私は同時にそこに6 Webブラウザを実行したい。各ブラウザには6つの異なるウェブサイトがあり、同時にその中に価値を埋めたいと思っています。 スレッドを使用すると、すべてのブラウザが実行され、ウェブサイトが正しくナビゲートされています。しかし、私の問題は、値が各ブラウザで1つずつ満たされていることです。私は各サイトで同時にすべての値を記入しても背景が(隠しモードでのスレッドの実行)背景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(); 
     } 
    } 

第2の機能をスレッドどのように私を提案してください

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親

です

ここでは、スペースが狭いために2つの関数しか入れません。

私はこれを使用しています * selectedwebsitesがリストあるスレッドを起動するには

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); 

*

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ブラウザがあります。私はまた、MDIの親である別のフォーム(frmBrowser)を取る。私は各関数(コードで示されている)にfrmbrowser1のインスタンスを作成し、それをfrmBrowserフォームの子にします。コードを確認してください。 – Neetesh

答えて

1

以下は、独自のスレッドでMethod WebSiteOneを実行するための裸の基本です。 スレッドを読み上げることをお勧めします。開始するには良い例がたくさんあります。

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

BackGroundWorkerを使用した更新。

+0

これは6つの異なるthraedsを作成するために使用しています – Neetesh

+0

スレッドm_thread1 = newスレッド(新しいThreadStart(hotfrog_com)); m_thread1.SetApartmentState(ApartmentState.STA); – Neetesh

+0

私はApplication.doEvent()を使用してナビゲートした後に1つ1つ完成するかもしれないと思います – Neetesh

1

おそらく1つのバックグラウンドスレッドを追加して、このスレッドで1つずつブラウザを開いているのでしょうか?

System.Threading.Threadpool.QueueUserWorkItem()を試してみて、これでブラウザに電話をかけてください。

+0

私は試してみてください......... – Neetesh

+0

は、すべての6つのWebサイトを6つのWebブラウザをナビゲートしますが、すべてのブラウザで詳細を同時に満たしません。それは一つ一つ。私はApplication.DOEventを使用してこれが原因である可能性があります – Neetesh

2

なぜこのためにブラウザを使用しますか?サーバーにHTTPリクエストを直接使用することはできませんか?ブラウザは、人間が自分の意見でフィールドに記入することになっている場合にのみ必要なUIを提供します。

あなたのケースでは、あなたが提出するウェブサイトがあなたが何らかの種類のウェブサービスとして記入したフォームを公開するのが最善の方法です(ただしこれが最良のシナリオです:))。

+0

私は約100のウェブサイトを持っており、私はソフトウェアを使用してそのデータを提出したい。だから私はスレッドを使用して、バックグラウンドスレッディングを使用したい – Neetesh

+0

あなたはあなたが自分自身を維持していると仮定して、この多くのWebサイトを持っている場合はなぜあなたは別のものを呼び出すのではなく、サービスにあなたの要求をすることができますwebserviceを公開しないでくださいあなたの要求をするためのプロセス。サービスがあれば、コードを読みやすく保守しやすくなります。 – thekip

+0

いいえ、私はウェブサイトを管理していません。 – Neetesh

1

問題は、すべての関連する要素を直ちにUIスレッドにマーシャリングするため、スレッドが役に立たないということです。私はあなたのコードを短くして見やすくしました。ワーカースレッドがすぐに再帰的に任意の実際の作業が起こる前に、UIスレッドの方法を再入力するControl.Invokeを呼び出す方法

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. 
    } 
} 

注意してください。また、ワーカースレッド自体が終了する前に、実際にUIスレッドが終了するのを実際に待機します。この効果は、新しいスレッドがリソースを浪費するだけで作成されているため、最初からUIスレッドですべてを行うよりも、より悪いです。

単純な解決策は、ワーカースレッドでControl.Invokeへの呼び出しを省略し、すべての作業を実行させることです。しかし、残念ながら、これはうまくいきません。なぜなら、有用な作業の大部分は、おそらくあなたが知っているように、UIスレッド以外のもので禁止されているUI要素への操作を必要とするからです。

戦略を根本的に変更する必要があります。

+0

その6つすべてのウェブサイトをナビゲートします。6ウェブブラウザは、すべてのブラウザで同時に詳細を入力しません。それは一つ一つ。私はApplication.DOEventを使用することがありますこれが原因です – Neetesh