2011-07-27 2 views
2

Je travaille sur une fenêtre C# de logiciel. Je veux courir là 6 web browser simultanément. Il y a 6 différents sites Web naviguent dans chaque navigateur et je veux remplir la valeur en eux simultanément. Utilisation de thread Tous les navigateurs sont en cours d'exécution et la navigation sur le site Web est correcte. Mais mon problème est que la valeur se remplit un par un dans chaque navigateur. S'il vous plaît me suggérer comment je remplir toute valeur simultanément dans chaque site et aussi le filetage arrière-plan (course de fil en mode caché)Fond Threading 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(); 
     } 
    } 

deuxième fonction

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

/* Ici frmBz est parent MDi

Ici, je ne mets que deux fonctions en raison de l'espace faible

J'utilise ceci

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

Pour démarrer la discussion * selectedwebsites est la liste *

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

Pourriez-vous montrer le code avec lequel vous rencontrez des problèmes? –

+0

Les navigateurs Web sont-ils hébergés sous une forme unique? –

+0

@Erno oui, j'ai pris un seul nom de formulaire (frmBrowser1) et il y a un webBrowser. Je prends aussi un autre formulaire (frmBrowser) qui est parent MDI. Je crée une instance de frmbrowser1 dans chaque fonction (qui est montrée dans le code) et en fait l'enfant de la forme frmBrowser. S'il vous plaît vérifier le code. – Neetesh

Répondre

1

Ce qui suit est les bases nues pour exécuter le Method WebSiteOne dans son propre fil. Je vous suggère de lire sur les discussions, il y a beaucoup de bons exemples pour commencer.

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

Mise à jour à l'aide de 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

celui-ci j'utilise pour créer 6 diffèrent thraeds – Neetesh

+0

Discussion m_thread1 = new Thread (nouveau ThreadStart (hotfrog_com)); m_thread1.SetApartmentState (ApartmentState.STA); – Neetesh

+0

Je pense qu'après avoir navigué j'utilise Application.doEvent() afin qu'il soit complet un par un – Neetesh

1

Peut-être avez-vous ajouté un fil de fond, puis ouvrez le navigateur un par un dans ce sujet?

Essayez System.Threading.Threadpool.QueueUserWorkItem(), puis placez votre appel dans le navigateur.

+0

ok j'essaie ......... – Neetesh

+0

son naviguer tous les 6 sites Web 6 navigateur Web dans mais pas remplir les détails dans tous les navigateurs simultanément. un par un. J'utilise Application.DOEvent peut être que c'est la cause – Neetesh

2

Pourquoi utilisez-vous un navigateur pour cela? Vous ne pouvez pas utiliser une requête HTTP directement sur le serveur? Les navigateurs ne servent qu'une interface utilisateur qui n'est nécessaire que si un «humain» est censé remplir les champs à mon avis.

Dans votre cas, il serait préférable que les sites Web auxquels vous soumettez des données exposent le formulaire que vous remplissez comme un service Web de quelque sorte (mais c'est le meilleur des cas :)).

+0

J'ai environ 100 sites Web et je veux soumettre des données en utilisant ce logiciel. Donc j'utilise des threads et je veux utiliser le threading de fond – Neetesh

+0

Si vous avez autant de sites web (en supposant que vous les mainteniez vous même) pourquoi n'exposez-vous pas un webservice pour pouvoir faire vos demandes à un service plutôt que d'en appeler un autre processus pour faire vos demandes. Avoir des services rendra votre code beaucoup plus facile à lire et à maintenir – thekip

+0

non je ne suis pas en train de maintenir des sites Web que je viens de les automatiser. – Neetesh

1

Le problème est que vos threads ne fonctionnent pas du tout puisqu'ils ramènent immédiatement tous les éléments pertinents sur le thread de l'interface utilisateur. J'ai court-circuité votre code ci-dessous pour le rendre plus facile à voir.

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

Remarquez comment le thread de travail appelle immédiatement Control.Invoke à réentrer récursive la méthode sur le thread d'interface utilisateur avant tout travail réel se produit. De plus, le thread de travail attend que le thread d'interface se termine avant que le thread de travail ne se termine. L'effet est pire que de simplement tout faire sur le thread de l'interface utilisateur depuis le début, car un nouveau thread a été créé pour rien de plus que de gaspiller des ressources.

La solution naïve serait d'omettre l'appel à Control.Invoke dans le thread de travail et de le laisser faire tout le travail. Mais hélas, cela ne fonctionnera pas non plus puisque la plupart du travail utile nécessite de faire des choses à des éléments de l'interface utilisateur qui, comme vous le savez probablement, est interdit sur autre chose qu'un thread d'interface utilisateur.

Vous allez devoir modifier radicalement votre stratégie.

+0

son naviguer tous les 6 sites Web 6 navigateur Web dans mais pas remplissant les détails dans tous les navigateurs simultanément. un par un. J'utilise Application.DOEvent peut être que c'est la cause – Neetesh