开发者

Background Threading C#

开发者 https://www.devze.com 2023-03-22 23:17 出处:网络
I am working on a software C# windows. I want to run there 6 web browser simultaneously. There are 6 different website navigate in each browser and I want to fill value in them simultaneously.

I am working on a software C# windows. I want to run there 6 web browser simultaneously. There are 6 different website navigate in each browser and I want to fill value in them simultaneously. Using thread all browser are running and website navigating correctly. But my problem is that value is filling one by one in each browser. Please suggest me how I fill all value simultaneously in each site and also background threading(thread run in hidden mode)

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("Basi开发者_开发问答cCompanyDetails").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();
        }
    }

Second Function

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

/* Here frmBz is MDi parent

Here i only put two function because of low space

I am using this

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

To start Thread *selectedwebsites is list*

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


Why do you use a browser for this? Can't you use a HTTP request directly to the server? Browsers just serve a UI which is only needed if a 'human' is supposed to fill out the fields in my opinion.

In your case it would be best if the websites your submitting data to would expose the form you're filling out as a webservice of some kind (however this is best case scenario :)).


The below is the bare basics to run the Method WebSiteOne in its own thread. I suggest you read up on threads, there are lots of good examples to get started.

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

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


Perhaps you have added one background thread and then open the browser one by one in this thread?

Try System.Threading.Threadpool.QueueUserWorkItem() and then put your call to the browser in this.


The problem is that your threads do no useful work at all since they immediately marshal all of the pertinent stuff back onto the UI thread. I have shorted your code below to make it easier to see.

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

Notice how the worker thread immediately calls Control.Invoke to recursively reenter the method on the UI thread before any real work happens. And on top of that the worker thread actually waits for the UI thread to finish before the worker thread itself terminates. The effect is worse1 than just doing everything on the UI thread from the very beginning since a new thread has been created to nothing more than waste resources.

The naive solution would be to omit the call to Control.Invoke in worker thread and let it do all of the work. But alas, this will not work either since most of the useful work requires doing stuff to UI elements which, as you probably know, is forbidden on anything other than a UI thread.

You are going to have to radically alter your strategy.

0

精彩评论

暂无评论...
验证码 换一张
取 消