基本上,我需要忙于等待,直到网页上出现一些html。我创建了以下代码忙等待对我来说:繁忙的等待线程
public void ExecuteBusyWaitThreads()
{
foreach (Canidate canidate in allCanidates)
{
Thread newThread = new Thread(delegate()
{
BusyWait(canidate);
});
newThread.Start();
}
}
public bool BusyWait(Canidate canidate)
{
//hit that url, and wait for the claim all button to appear
string page = null;
while (found == false)
{
HttpWebRequest request = Canidate.GetHTTPRequest(canidate.URL);
//make sure we add the authentication cookes to the request
request = Canidate.AddCookiesToRequest(request, canidate.GetCookies());
page = new Canidate().GetPage(request);
if (page.ToLower().Contains("claim all"))
{
found = true;
NotifyAllThreads();
}
}
return true;
}
所以,如果我有8 canidates
,它会催生关闭8个线程,每个正在寻找claim all
出现在网页上。 found
是一个全局变量。一旦其中一个线程发现claim all
,他们都应该保释。
我对这种方法有几个问题。首先,它是一个好方法。其次,每个线程都会获得自己的忙等待函数的“副本”。我的意思是,一个线程可以抢占另一个线程并更改该函数中的数据,或者它们每个都获得函数内声明的变量的副本。请注意,这两个函数都在同一个对象内。
有效的技术问题,但看起来像试图在扑克或拍卖作弊。 – 2012-03-08 18:21:28