询问者
多线程webBrowser的AccessViolationException异常

问题
-
采用多个线程分别控制一个webBrowser,同时加载不同网页,然后读取其中的Document。可是会不定期地抛AccessViolationException异常. 抛出异常后,如果在Debug中,直接F5执行下面语句不会对程序结果又任何影响,但是还是会不定期的再次抛出这个异常
void webBrowser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
//throw new NotImplementedException();
if(((WebBrowser)sender).ReadyState<WebBrowserReadyState.Complete)return;
System.Windows.Forms.HtmlDocument document = webBrowser.Document; //就是在这句访问webBrowser的时候抛出异常,
shopInfoQueue.Enqueue(shopInfo);
htmlContentQueue.Enqueue(document.Body.InnerHtml);}
不明白为什么会不定期出错,并且继续执行并不影响结果,最主要的,这个怎么解决,或者能否屏蔽该异常
全部回复
-
WebBrowser控件本身是STA组件,所以,创建WebBrowser的线程需要是STA。通常,UI组件是在UI线程中初始化,所以,建议把Main方法设为STA再试试。如果还不行,再想别的方法。
设置主线程的ApartmentState, Thread.CurrentThread.SetApartmentState(ApartmentState.STA),会抛出InvalidOperationException异常,Failed to set the specified COM apartment state....
是设置的方法不对?还是??
-
应该是多线程调用WebBrowser出错了,能多贴一些你的代码,或者传个能重现问题的小Project么?
public void Init() //每个线程有自己的form和WebBrowser
{
lock (lockMemory)
{
this.form = new Form();
this.webBrowser = new WebBrowser();
this.form.Controls.Add(webBrowser);
}
form.WindowState = FormWindowState.Minimized;
form.Visible = false;
form.ShowInTaskbar = false;
this.webBrowser.ScriptErrorsSuppressed = true;
this.webBrowser.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(webBrowser_DocumentCompleted);Begin();
}public void Begin()
{lock (lockProgram)
{
cnt = Program.shopListOffset++;
if (cnt >= Program.totalCount)
{
Console.WriteLine("Finish!");
System.Threading.Thread.CurrentThread.Abort();
}
shopInfo = Program.shopInfoQueue.Dequeue(); //从队列中取URL
}this.webBrowser.Navigate(getUrl(shopInfo));
if(form.Visible==false)
form.ShowDialog();
}void webBrowser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
if (((WebBrowser)sender).ReadyState < WebBrowserReadyState.Complete)
return;
waitSomeTime();
}public void waitSomeTime() //需要在页面加载完成后等待2秒才能取到要取的内容
{
timer = new System.Windows.Forms.Timer();
timer.Interval = 2000; //2秒啓動
timer.Tick += new EventHandler(Timer_Tick);
timer.Start();
}
private void Timer_Tick(object sender, EventArgs e)
{
lock (lockHtmlContentQueue)
{
System.Windows.Forms.HtmlDocument document= this.webBrowser.Document; //这句出错
shopInfoQueue.Enqueue(shopInfo);
htmlContentQueue.Enqueue(document.Body.InnerHtml);}
Begin();
}
这些就是一个线程要做的事情,大概就是从队列里取出URL,打开网页,等两秒钟然后取出网页的Document,最后把document.Body.InnerHtml存到另一个队列,然后再重新开始这么一个过程。
多谢指点了
-
WebBrowser需要创建在UI线程中,且该线程中需要有消息循环。你能把Init()的调用的代码贴出来么?每个线程怎么创建,是否有消息循环,都很重要。
static void Main(string[] args)
{
int maxThreadCount = Int32.Parse(Console.ReadLine());System.Threading.Thread[] t = new Thread[maxThreadCount];
GoogleMap[] gmc = new GoogleMap[maxThreadCount];
for (i = 0; i < maxThreadCount; i++)
{
gm[i] = new GoogleMap();
t[i] = new Thread(gm[i].Init);
t[i].SetApartmentState(System.Threading.ApartmentState.STA);
t[i].Start();
}}
-
我大概试了一下,总是在document.body.InnerHTML这句出的错。document是好的,document.body是空。
不知道怎么回事,所以把我的代码帖上来,你看看,能不能修改修改重现你的问题。maxThreadCount = 15.
googlemap.cs
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Windows.Forms; namespace ForumMultiThreadWebBrowser { class GoogleMap { public GoogleMap() { this.lockHtmlContentQueue = new Object(); this.lockMemory = new Object(); this.lockProgram = new Object(); this.htmlContentQueue = new Queue<string>(); } public void Init() //每个线程有自己的form和WebBrowser { lock (lockMemory) { this.form = new Form(); this.webBrowser = new WebBrowser(); this.form.Controls.Add(webBrowser); } form.WindowState = FormWindowState.Minimized; form.Visible = false; form.ShowInTaskbar = false; this.webBrowser.ScriptErrorsSuppressed = true; this.webBrowser.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(webBrowser_DocumentCompleted); Begin(); } public void Begin() { lock (lockProgram) { int cnt = Program.shopListOffset++; if (cnt >= Program.totalCount) { Console.WriteLine("Finish!"); System.Threading.Thread.CurrentThread.Abort(); } shopInfo = Program.shopInfoQueue.Dequeue(); //从队列中取URL } this.webBrowser.Navigate(getUrl(shopInfo)); if (form.Visible == false) form.ShowDialog(); } void webBrowser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e) { if (((WebBrowser)sender).ReadyState < WebBrowserReadyState.Complete) return; waitSomeTime(); } public void waitSomeTime() //需要在页面加载完成后等待2秒才能取到要取的内容 { timer = new System.Windows.Forms.Timer(); timer.Interval = 2000; //2秒啓動 timer.Tick += new EventHandler(Timer_Tick); timer.Start(); } private void Timer_Tick(object sender, EventArgs e) { lock (lockHtmlContentQueue) { System.Windows.Forms.HtmlDocument document = this.webBrowser.Document; //这句出错 Program.shopInfoQueue.Enqueue(shopInfo); this.htmlContentQueue.Enqueue(document.Body.InnerHtml); } Begin(); } private string getUrl(ShopInfo shopinfo) { return shopinfo.URL; } #region Fields private object lockMemory; private object lockProgram; private object lockHtmlContentQueue; private WebBrowser webBrowser; private Form form; private Timer timer; private ShopInfo shopInfo; private Queue<string> htmlContentQueue; #endregion } }
ShopInfo.cs
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ForumMultiThreadWebBrowser { class ShopInfo { public ShopInfo(string url) { this.Url = url; } public string URL { get { return this.Url; } } private string Url; } }
Program.cs
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading; using System.Threading.Tasks; namespace ForumMultiThreadWebBrowser { class Program { static void Main(string[] args) { Program.shopListOffset = 0; Program.shopInfoQueue = new Queue<ShopInfo>(); Program.shopInfoQueue.Enqueue(new ShopInfo("http://bing.com")); Program.shopInfoQueue.Enqueue(new ShopInfo("http://google.com")); Program.shopInfoQueue.Enqueue(new ShopInfo("http://microsoft.com")); Program.shopInfoQueue.Enqueue(new ShopInfo("http://cnn.com")); Program.shopInfoQueue.Enqueue(new ShopInfo("http://facebook.com")); Program.shopInfoQueue.Enqueue(new ShopInfo("http://bing.com")); Program.shopInfoQueue.Enqueue(new ShopInfo("http://google.com")); Program.shopInfoQueue.Enqueue(new ShopInfo("http://microsoft.com")); Program.shopInfoQueue.Enqueue(new ShopInfo("http://cnn.com")); Program.shopInfoQueue.Enqueue(new ShopInfo("http://facebook.com")); Program.shopInfoQueue.Enqueue(new ShopInfo("http://bing.com")); Program.shopInfoQueue.Enqueue(new ShopInfo("http://google.com")); Program.shopInfoQueue.Enqueue(new ShopInfo("http://microsoft.com")); Program.shopInfoQueue.Enqueue(new ShopInfo("http://cnn.com")); Program.shopInfoQueue.Enqueue(new ShopInfo("http://facebook.com")); Program.shopInfoQueue.Enqueue(new ShopInfo("http://bing.com")); Program.shopInfoQueue.Enqueue(new ShopInfo("http://google.com")); Program.shopInfoQueue.Enqueue(new ShopInfo("http://microsoft.com")); Program.shopInfoQueue.Enqueue(new ShopInfo("http://cnn.com")); Program.shopInfoQueue.Enqueue(new ShopInfo("http://facebook.com")); Program.shopInfoQueue.Enqueue(new ShopInfo("http://bing.com")); Program.shopInfoQueue.Enqueue(new ShopInfo("http://google.com")); Program.shopInfoQueue.Enqueue(new ShopInfo("http://microsoft.com")); Program.shopInfoQueue.Enqueue(new ShopInfo("http://cnn.com")); Program.shopInfoQueue.Enqueue(new ShopInfo("http://facebook.com")); Program.shopInfoQueue.Enqueue(new ShopInfo("http://bing.com")); Program.shopInfoQueue.Enqueue(new ShopInfo("http://google.com")); Program.shopInfoQueue.Enqueue(new ShopInfo("http://microsoft.com")); Program.shopInfoQueue.Enqueue(new ShopInfo("http://cnn.com")); Program.shopInfoQueue.Enqueue(new ShopInfo("http://facebook.com")); Program.shopInfoQueue.Enqueue(new ShopInfo("http://bing.com")); Program.shopInfoQueue.Enqueue(new ShopInfo("http://google.com")); Program.shopInfoQueue.Enqueue(new ShopInfo("http://microsoft.com")); Program.shopInfoQueue.Enqueue(new ShopInfo("http://cnn.com")); Program.shopInfoQueue.Enqueue(new ShopInfo("http://facebook.com")); Program.shopInfoQueue.Enqueue(new ShopInfo("http://bing.com")); Program.shopInfoQueue.Enqueue(new ShopInfo("http://google.com")); Program.shopInfoQueue.Enqueue(new ShopInfo("http://microsoft.com")); Program.shopInfoQueue.Enqueue(new ShopInfo("http://cnn.com")); Program.shopInfoQueue.Enqueue(new ShopInfo("http://facebook.com")); Program.shopInfoQueue.Enqueue(new ShopInfo("http://bing.com")); Program.shopInfoQueue.Enqueue(new ShopInfo("http://google.com")); Program.shopInfoQueue.Enqueue(new ShopInfo("http://microsoft.com")); Program.shopInfoQueue.Enqueue(new ShopInfo("http://cnn.com")); Program.shopInfoQueue.Enqueue(new ShopInfo("http://facebook.com")); Program.shopInfoQueue.Enqueue(new ShopInfo("http://bing.com")); Program.shopInfoQueue.Enqueue(new ShopInfo("http://google.com")); Program.shopInfoQueue.Enqueue(new ShopInfo("http://microsoft.com")); Program.shopInfoQueue.Enqueue(new ShopInfo("http://cnn.com")); Program.shopInfoQueue.Enqueue(new ShopInfo("http://facebook.com")); Program.shopInfoQueue.Enqueue(new ShopInfo("http://bing.com")); Program.shopInfoQueue.Enqueue(new ShopInfo("http://google.com")); Program.shopInfoQueue.Enqueue(new ShopInfo("http://microsoft.com")); Program.shopInfoQueue.Enqueue(new ShopInfo("http://cnn.com")); Program.shopInfoQueue.Enqueue(new ShopInfo("http://facebook.com")); Program.shopInfoQueue.Enqueue(new ShopInfo("http://bing.com")); Program.shopInfoQueue.Enqueue(new ShopInfo("http://google.com")); Program.shopInfoQueue.Enqueue(new ShopInfo("http://microsoft.com")); Program.shopInfoQueue.Enqueue(new ShopInfo("http://cnn.com")); Program.shopInfoQueue.Enqueue(new ShopInfo("http://facebook.com")); Program.shopInfoQueue.Enqueue(new ShopInfo("http://bing.com")); Program.shopInfoQueue.Enqueue(new ShopInfo("http://google.com")); Program.shopInfoQueue.Enqueue(new ShopInfo("http://microsoft.com")); Program.shopInfoQueue.Enqueue(new ShopInfo("http://cnn.com")); Program.shopInfoQueue.Enqueue(new ShopInfo("http://facebook.com")); Program.shopInfoQueue.Enqueue(new ShopInfo("http://bing.com")); Program.shopInfoQueue.Enqueue(new ShopInfo("http://google.com")); Program.shopInfoQueue.Enqueue(new ShopInfo("http://microsoft.com")); Program.shopInfoQueue.Enqueue(new ShopInfo("http://cnn.com")); Program.shopInfoQueue.Enqueue(new ShopInfo("http://facebook.com")); Program.shopInfoQueue.Enqueue(new ShopInfo("http://bing.com")); Program.shopInfoQueue.Enqueue(new ShopInfo("http://google.com")); Program.shopInfoQueue.Enqueue(new ShopInfo("http://microsoft.com")); Program.shopInfoQueue.Enqueue(new ShopInfo("http://cnn.com")); Program.shopInfoQueue.Enqueue(new ShopInfo("http://facebook.com")); Program.shopInfoQueue.Enqueue(new ShopInfo("http://bing.com")); Program.shopInfoQueue.Enqueue(new ShopInfo("http://google.com")); Program.shopInfoQueue.Enqueue(new ShopInfo("http://microsoft.com")); Program.shopInfoQueue.Enqueue(new ShopInfo("http://cnn.com")); Program.shopInfoQueue.Enqueue(new ShopInfo("http://facebook.com")); Program.shopInfoQueue.Enqueue(new ShopInfo("http://bing.com")); Program.shopInfoQueue.Enqueue(new ShopInfo("http://google.com")); Program.shopInfoQueue.Enqueue(new ShopInfo("http://microsoft.com")); Program.shopInfoQueue.Enqueue(new ShopInfo("http://cnn.com")); Program.shopInfoQueue.Enqueue(new ShopInfo("http://facebook.com")); Program.shopInfoQueue.Enqueue(new ShopInfo("http://bing.com")); Program.shopInfoQueue.Enqueue(new ShopInfo("http://google.com")); Program.shopInfoQueue.Enqueue(new ShopInfo("http://microsoft.com")); Program.shopInfoQueue.Enqueue(new ShopInfo("http://cnn.com")); Program.shopInfoQueue.Enqueue(new ShopInfo("http://facebook.com")); Program.shopInfoQueue.Enqueue(new ShopInfo("http://bing.com")); Program.shopInfoQueue.Enqueue(new ShopInfo("http://google.com")); Program.shopInfoQueue.Enqueue(new ShopInfo("http://microsoft.com")); Program.shopInfoQueue.Enqueue(new ShopInfo("http://cnn.com")); Program.shopInfoQueue.Enqueue(new ShopInfo("http://facebook.com")); Program.shopInfoQueue.Enqueue(new ShopInfo("http://bing.com")); Program.shopInfoQueue.Enqueue(new ShopInfo("http://google.com")); Program.shopInfoQueue.Enqueue(new ShopInfo("http://microsoft.com")); Program.shopInfoQueue.Enqueue(new ShopInfo("http://cnn.com")); Program.shopInfoQueue.Enqueue(new ShopInfo("http://facebook.com")); Program.shopInfoQueue.Enqueue(new ShopInfo("http://bing.com")); Program.shopInfoQueue.Enqueue(new ShopInfo("http://google.com")); Program.shopInfoQueue.Enqueue(new ShopInfo("http://microsoft.com")); Program.shopInfoQueue.Enqueue(new ShopInfo("http://cnn.com")); Program.shopInfoQueue.Enqueue(new ShopInfo("http://facebook.com")); Program.shopInfoQueue.Enqueue(new ShopInfo("http://bing.com")); Program.shopInfoQueue.Enqueue(new ShopInfo("http://google.com")); Program.shopInfoQueue.Enqueue(new ShopInfo("http://microsoft.com")); Program.shopInfoQueue.Enqueue(new ShopInfo("http://cnn.com")); Program.shopInfoQueue.Enqueue(new ShopInfo("http://facebook.com")); Program.shopInfoQueue.Enqueue(new ShopInfo("http://bing.com")); Program.shopInfoQueue.Enqueue(new ShopInfo("http://google.com")); Program.shopInfoQueue.Enqueue(new ShopInfo("http://microsoft.com")); Program.shopInfoQueue.Enqueue(new ShopInfo("http://cnn.com")); Program.shopInfoQueue.Enqueue(new ShopInfo("http://facebook.com")); Program.shopInfoQueue.Enqueue(new ShopInfo("http://bing.com")); Program.shopInfoQueue.Enqueue(new ShopInfo("http://google.com")); Program.shopInfoQueue.Enqueue(new ShopInfo("http://microsoft.com")); Program.shopInfoQueue.Enqueue(new ShopInfo("http://cnn.com")); Program.shopInfoQueue.Enqueue(new ShopInfo("http://facebook.com")); Program.shopInfoQueue.Enqueue(new ShopInfo("http://bing.com")); Program.shopInfoQueue.Enqueue(new ShopInfo("http://google.com")); Program.shopInfoQueue.Enqueue(new ShopInfo("http://microsoft.com")); Program.shopInfoQueue.Enqueue(new ShopInfo("http://cnn.com")); Program.shopInfoQueue.Enqueue(new ShopInfo("http://facebook.com")); Program.shopInfoQueue.Enqueue(new ShopInfo("http://bing.com")); Program.shopInfoQueue.Enqueue(new ShopInfo("http://google.com")); Program.shopInfoQueue.Enqueue(new ShopInfo("http://microsoft.com")); Program.shopInfoQueue.Enqueue(new ShopInfo("http://cnn.com")); Program.shopInfoQueue.Enqueue(new ShopInfo("http://facebook.com")); Program.shopInfoQueue.Enqueue(new ShopInfo("http://bing.com")); Program.shopInfoQueue.Enqueue(new ShopInfo("http://google.com")); Program.shopInfoQueue.Enqueue(new ShopInfo("http://microsoft.com")); Program.shopInfoQueue.Enqueue(new ShopInfo("http://cnn.com")); Program.shopInfoQueue.Enqueue(new ShopInfo("http://facebook.com")); Program.shopInfoQueue.Enqueue(new ShopInfo("http://bing.com")); Program.shopInfoQueue.Enqueue(new ShopInfo("http://google.com")); Program.shopInfoQueue.Enqueue(new ShopInfo("http://microsoft.com")); Program.shopInfoQueue.Enqueue(new ShopInfo("http://cnn.com")); Program.shopInfoQueue.Enqueue(new ShopInfo("http://facebook.com")); Program.shopInfoQueue.Enqueue(new ShopInfo("http://bing.com")); Program.shopInfoQueue.Enqueue(new ShopInfo("http://google.com")); Program.shopInfoQueue.Enqueue(new ShopInfo("http://microsoft.com")); Program.shopInfoQueue.Enqueue(new ShopInfo("http://cnn.com")); Program.shopInfoQueue.Enqueue(new ShopInfo("http://facebook.com")); Program.shopInfoQueue.Enqueue(new ShopInfo("http://bing.com")); Program.shopInfoQueue.Enqueue(new ShopInfo("http://google.com")); Program.shopInfoQueue.Enqueue(new ShopInfo("http://microsoft.com")); Program.shopInfoQueue.Enqueue(new ShopInfo("http://cnn.com")); Program.shopInfoQueue.Enqueue(new ShopInfo("http://facebook.com")); Program.shopInfoQueue.Enqueue(new ShopInfo("http://bing.com")); Program.shopInfoQueue.Enqueue(new ShopInfo("http://google.com")); Program.shopInfoQueue.Enqueue(new ShopInfo("http://microsoft.com")); Program.shopInfoQueue.Enqueue(new ShopInfo("http://cnn.com")); Program.shopInfoQueue.Enqueue(new ShopInfo("http://facebook.com")); Program.shopInfoQueue.Enqueue(new ShopInfo("http://bing.com")); Program.shopInfoQueue.Enqueue(new ShopInfo("http://google.com")); Program.shopInfoQueue.Enqueue(new ShopInfo("http://microsoft.com")); Program.shopInfoQueue.Enqueue(new ShopInfo("http://cnn.com")); Program.shopInfoQueue.Enqueue(new ShopInfo("http://facebook.com")); Program.shopInfoQueue.Enqueue(new ShopInfo("http://bing.com")); Program.shopInfoQueue.Enqueue(new ShopInfo("http://google.com")); Program.shopInfoQueue.Enqueue(new ShopInfo("http://microsoft.com")); Program.shopInfoQueue.Enqueue(new ShopInfo("http://cnn.com")); Program.shopInfoQueue.Enqueue(new ShopInfo("http://facebook.com")); Program.shopInfoQueue.Enqueue(new ShopInfo("http://bing.com")); Program.shopInfoQueue.Enqueue(new ShopInfo("http://google.com")); Program.shopInfoQueue.Enqueue(new ShopInfo("http://microsoft.com")); Program.shopInfoQueue.Enqueue(new ShopInfo("http://cnn.com")); Program.shopInfoQueue.Enqueue(new ShopInfo("http://facebook.com")); Program.shopInfoQueue.Enqueue(new ShopInfo("http://bing.com")); Program.shopInfoQueue.Enqueue(new ShopInfo("http://google.com")); Program.shopInfoQueue.Enqueue(new ShopInfo("http://microsoft.com")); Program.shopInfoQueue.Enqueue(new ShopInfo("http://cnn.com")); Program.shopInfoQueue.Enqueue(new ShopInfo("http://facebook.com")); Program.shopInfoQueue.Enqueue(new ShopInfo("http://bing.com")); Program.shopInfoQueue.Enqueue(new ShopInfo("http://google.com")); Program.shopInfoQueue.Enqueue(new ShopInfo("http://microsoft.com")); Program.shopInfoQueue.Enqueue(new ShopInfo("http://cnn.com")); Program.shopInfoQueue.Enqueue(new ShopInfo("http://facebook.com")); Program.shopInfoQueue.Enqueue(new ShopInfo("http://bing.com")); Program.shopInfoQueue.Enqueue(new ShopInfo("http://google.com")); Program.shopInfoQueue.Enqueue(new ShopInfo("http://microsoft.com")); Program.shopInfoQueue.Enqueue(new ShopInfo("http://cnn.com")); Program.shopInfoQueue.Enqueue(new ShopInfo("http://facebook.com")); Program.shopInfoQueue.Enqueue(new ShopInfo("http://bing.com")); Program.shopInfoQueue.Enqueue(new ShopInfo("http://google.com")); Program.shopInfoQueue.Enqueue(new ShopInfo("http://microsoft.com")); Program.shopInfoQueue.Enqueue(new ShopInfo("http://cnn.com")); Program.shopInfoQueue.Enqueue(new ShopInfo("http://facebook.com")); Program.shopInfoQueue.Enqueue(new ShopInfo("http://bing.com")); Program.shopInfoQueue.Enqueue(new ShopInfo("http://google.com")); Program.shopInfoQueue.Enqueue(new ShopInfo("http://microsoft.com")); Program.shopInfoQueue.Enqueue(new ShopInfo("http://cnn.com")); Program.shopInfoQueue.Enqueue(new ShopInfo("http://facebook.com")); Program.shopInfoQueue.Enqueue(new ShopInfo("http://bing.com")); Program.shopInfoQueue.Enqueue(new ShopInfo("http://google.com")); Program.shopInfoQueue.Enqueue(new ShopInfo("http://microsoft.com")); Program.shopInfoQueue.Enqueue(new ShopInfo("http://cnn.com")); Program.shopInfoQueue.Enqueue(new ShopInfo("http://facebook.com")); Program.shopInfoQueue.Enqueue(new ShopInfo("http://bing.com")); Program.shopInfoQueue.Enqueue(new ShopInfo("http://google.com")); Program.shopInfoQueue.Enqueue(new ShopInfo("http://microsoft.com")); Program.shopInfoQueue.Enqueue(new ShopInfo("http://cnn.com")); Program.shopInfoQueue.Enqueue(new ShopInfo("http://facebook.com")); Program.shopInfoQueue.Enqueue(new ShopInfo("http://bing.com")); Program.shopInfoQueue.Enqueue(new ShopInfo("http://google.com")); Program.shopInfoQueue.Enqueue(new ShopInfo("http://microsoft.com")); Program.shopInfoQueue.Enqueue(new ShopInfo("http://cnn.com")); Program.shopInfoQueue.Enqueue(new ShopInfo("http://facebook.com")); Program.shopInfoQueue.Enqueue(new ShopInfo("http://bing.com")); Program.shopInfoQueue.Enqueue(new ShopInfo("http://google.com")); Program.shopInfoQueue.Enqueue(new ShopInfo("http://microsoft.com")); Program.shopInfoQueue.Enqueue(new ShopInfo("http://cnn.com")); Program.shopInfoQueue.Enqueue(new ShopInfo("http://facebook.com")); Program.shopInfoQueue.Enqueue(new ShopInfo("http://bing.com")); Program.shopInfoQueue.Enqueue(new ShopInfo("http://google.com")); Program.shopInfoQueue.Enqueue(new ShopInfo("http://microsoft.com")); Program.shopInfoQueue.Enqueue(new ShopInfo("http://cnn.com")); Program.shopInfoQueue.Enqueue(new ShopInfo("http://facebook.com")); Program.shopInfoQueue.Enqueue(new ShopInfo("http://bing.com")); Program.shopInfoQueue.Enqueue(new ShopInfo("http://google.com")); Program.shopInfoQueue.Enqueue(new ShopInfo("http://microsoft.com")); Program.shopInfoQueue.Enqueue(new ShopInfo("http://cnn.com")); Program.shopInfoQueue.Enqueue(new ShopInfo("http://facebook.com")); Program.shopInfoQueue.Enqueue(new ShopInfo("http://bing.com")); Program.shopInfoQueue.Enqueue(new ShopInfo("http://google.com")); Program.shopInfoQueue.Enqueue(new ShopInfo("http://microsoft.com")); Program.shopInfoQueue.Enqueue(new ShopInfo("http://cnn.com")); Program.shopInfoQueue.Enqueue(new ShopInfo("http://facebook.com")); Program.shopInfoQueue.Enqueue(new ShopInfo("http://bing.com")); Program.shopInfoQueue.Enqueue(new ShopInfo("http://google.com")); Program.shopInfoQueue.Enqueue(new ShopInfo("http://microsoft.com")); Program.shopInfoQueue.Enqueue(new ShopInfo("http://cnn.com")); Program.shopInfoQueue.Enqueue(new ShopInfo("http://facebook.com")); Program.shopInfoQueue.Enqueue(new ShopInfo("http://bing.com")); Program.shopInfoQueue.Enqueue(new ShopInfo("http://google.com")); Program.shopInfoQueue.Enqueue(new ShopInfo("http://microsoft.com")); Program.shopInfoQueue.Enqueue(new ShopInfo("http://cnn.com")); Program.shopInfoQueue.Enqueue(new ShopInfo("http://facebook.com")); Program.shopInfoQueue.Enqueue(new ShopInfo("http://bing.com")); Program.shopInfoQueue.Enqueue(new ShopInfo("http://google.com")); Program.shopInfoQueue.Enqueue(new ShopInfo("http://microsoft.com")); Program.shopInfoQueue.Enqueue(new ShopInfo("http://cnn.com")); Program.shopInfoQueue.Enqueue(new ShopInfo("http://facebook.com")); Program.shopInfoQueue.Enqueue(new ShopInfo("http://bing.com")); Program.shopInfoQueue.Enqueue(new ShopInfo("http://google.com")); Program.shopInfoQueue.Enqueue(new ShopInfo("http://microsoft.com")); Program.shopInfoQueue.Enqueue(new ShopInfo("http://cnn.com")); Program.shopInfoQueue.Enqueue(new ShopInfo("http://facebook.com")); Program.shopInfoQueue.Enqueue(new ShopInfo("http://bing.com")); Program.shopInfoQueue.Enqueue(new ShopInfo("http://google.com")); Program.shopInfoQueue.Enqueue(new ShopInfo("http://microsoft.com")); Program.shopInfoQueue.Enqueue(new ShopInfo("http://cnn.com")); Program.shopInfoQueue.Enqueue(new ShopInfo("http://facebook.com")); Program.shopInfoQueue.Enqueue(new ShopInfo("http://bing.com")); Program.shopInfoQueue.Enqueue(new ShopInfo("http://google.com")); Program.shopInfoQueue.Enqueue(new ShopInfo("http://microsoft.com")); Program.shopInfoQueue.Enqueue(new ShopInfo("http://cnn.com")); Program.shopInfoQueue.Enqueue(new ShopInfo("http://facebook.com")); Program.shopInfoQueue.Enqueue(new ShopInfo("http://bing.com")); Program.shopInfoQueue.Enqueue(new ShopInfo("http://google.com")); Program.shopInfoQueue.Enqueue(new ShopInfo("http://microsoft.com")); Program.shopInfoQueue.Enqueue(new ShopInfo("http://cnn.com")); Program.shopInfoQueue.Enqueue(new ShopInfo("http://facebook.com")); Program.shopInfoQueue.Enqueue(new ShopInfo("http://bing.com")); Program.shopInfoQueue.Enqueue(new ShopInfo("http://google.com")); Program.shopInfoQueue.Enqueue(new ShopInfo("http://microsoft.com")); Program.shopInfoQueue.Enqueue(new ShopInfo("http://cnn.com")); Program.shopInfoQueue.Enqueue(new ShopInfo("http://facebook.com")); Program.shopInfoQueue.Enqueue(new ShopInfo("http://bing.com")); Program.shopInfoQueue.Enqueue(new ShopInfo("http://google.com")); Program.shopInfoQueue.Enqueue(new ShopInfo("http://microsoft.com")); Program.shopInfoQueue.Enqueue(new ShopInfo("http://cnn.com")); Program.shopInfoQueue.Enqueue(new ShopInfo("http://facebook.com")); Program.totalCount = Program.shopInfoQueue.Count; int maxThreadCount = Int32.Parse(Console.ReadLine()); System.Threading.Thread[] t = new Thread[maxThreadCount]; GoogleMap[] gm = new GoogleMap[maxThreadCount]; for (int i = 0; i < maxThreadCount; i++) { gm[i] = new GoogleMap(); t[i] = new Thread(gm[i].Init); t[i].SetApartmentState(System.Threading.ApartmentState.STA); t[i].Start(); } } public static int shopListOffset; public static int totalCount; public static Queue<ShopInfo> shopInfoQueue; } }
-
Program.shopInfoQueue.Enqueue(new ShopInfo("http://www.dianping.com/shop/5438066"));
Program.shopInfoQueue.Enqueue(new ShopInfo("http://www.dianping.com/shop/4227576"));
Program.shopInfoQueue.Enqueue(new ShopInfo("http://www.dianping.com/shop/1989367"));
Program.shopInfoQueue.Enqueue(new ShopInfo("http://www.dianping.com/shop/5646380"));
Program.shopInfoQueue.Enqueue(new ShopInfo("http://www.dianping.com/shop/1990279"));你把url换成这些,然后数据量大一些,大概到5000条左右吧,应该就会出我说的那个错误了
多谢指点了,嘿嘿