[英]Background Threading C#
我正在使用C#Windows軟件。 我想同時運行6個Web瀏覽器。 每個瀏覽器中都有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);
要啟動Thread * 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) {}
}
為什么要為此使用瀏覽器? 您不能直接向服務器使用HTTP請求嗎? 瀏覽器只提供一個UI,僅在我認為應該由“人類”填寫字段時才需要。
對於您而言,最好是您提交數據的網站將您要填寫的表單公開為某種Web服務(但是,這是最佳情況:)。
下面是在自己的線程中運行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();
}
也許您已經添加了一個后台線程,然后在該線程中一一打開瀏覽器?
嘗試使用System.Threading.Threadpool.QueueUserWorkItem()
,然后在其中調用瀏覽器。
問題在於您的線程根本不做任何有用的工作,因為它們會立即將所有相關的內容編組回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線程上一切變得更糟1。
天真的解決方案是忽略對工作線程中Control.Invoke
的調用,然后讓它完成所有工作。 但是,a,這還是行不通的,因為大多數有用的工作都需要對UI元素做一些事情,您可能已經知道,UI元素是除UI線程以外的任何事情所禁止的。
您將不得不從根本上改變您的策略。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.