2011-07-27 53 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