c# - 后台线程 C#

标签 c# multithreading c#-4.0

我正在开发一个 C# windows 软件。我想同时运行 6 个网络浏览器。每个浏览器中有 6 个不同的网站导航,我想同时在它们中填充值。 使用线程,所有浏览器都在运行并且网站导航正确。但我的问题是,值在每个浏览器中都被一一填充。请建议我如何在每个站点中同时填充所有值以及后台线程(线程在隐藏模式下运行)

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

启动线程 *所选网站已列出*

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) {}
 }

最佳答案

为什么要使用浏览器来实现此目的?不能直接使用HTTP请求到服务器吗?浏览器只是提供一个用户界面,只有在我看来应该由“人”填写字段时才需要该用户界面。

就您而言,最好是您提交数据的网站将您填写的表单公开为某种网络服务(不过这是最好的情况:))。

关于c# - 后台线程 C#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6840327/

相关文章:

c# - 如何使用 Simple Injector 注册命名实例

c# - WPF 的 DI 工厂

.net - 如何使用 .Net EF 从 MySql TinyInt(1) 列中读取大于 1 的值?

c# - 在 linq 中将实体类型作为参数传递

.net - 填充 ListView 多列

c# - 是否有等同于 "Application.OpenForms"的 Silverlight?

c# - 枚举类型的值

c# - C# 4.0 中用于文件加载的多线程

android - 执行两个 Runnables Android

c - 关于套接字/tcp 连接的多线程的问题