【发布时间】:2011-04-22 13:39:50
【问题描述】:
我有在我的机器人中使用的 webClient:
using System;
using System.Net;
namespace Game_Bot
{
class WebClientEx : WebClient
{
public CookieContainer CookieContainer { get; private set; }
public WebClientEx()
{
CookieContainer = new CookieContainer();
}
public void ClearCookies()
{
CookieContainer = new CookieContainer();
}
protected override WebRequest GetWebRequest(Uri address)
{
var request = base.GetWebRequest(address);
if (request is HttpWebRequest)
{
(request as HttpWebRequest).CookieContainer = CookieContainer;
}
return request;
}
}
}
我的 webclient 有一个对象。 有很多方法可以使用它。如果两个线程想使用该 webClient 下载,我收到错误消息说 webClent 一次只能运行一个操作。 如何修改该类,以便当一个线程正在使用它时,另一个线程必须等待。 我需要以某种方式锁定它。
【问题讨论】:
-
既然你不想让它们同时运行,为什么不使用队列呢? dotnetperls.com/queue
-
@Prix 所有线程都在做任务,等待,计数。有时这些线程需要从页面中加载一些 html。我非常罕见,但有时会遇到 2 个线程想要同时下载页面。我想锁定它,以便当时只有一个人可以使用该 WebClient。
标签: c# .net multithreading locking webclient