这是我每秒调用的方法,但是当我测量完成它所需的时间时,有时需要不到一秒钟,但有时最多需要 4 分钟。
HashSet<int> Handles = new HashSet<int> ( );
void MonitorWebsite ( )
{
string linkIdPrefix = "thread_title_";
foreach ( var lnk in this.Links )
{
doc = web.Load ( lnk );
var threadLinks = doc.DocumentNode.Descendants ( "a" )
.Where ( link => link.Id.StartsWith ( linkIdPrefix ) );
foreach ( var topic in threadLinks )
{
string id = topic.Id.Substring ( linkIdPrefix.Length ); // remove "thread_title_"
string name = topic.InnerHtml; // or link.InnerText
int handle = Convert.ToInt32 ( id );
if ( this.Handles.Contains ( handle ) == false && name.IndexOf ( "keyword", StringComparison.OrdinalIgnoreCase ) >= 0 )
{
string href = topic.GetAttributeValue ( "href", null );
this.OpenInBrowser ( href );
SoundPlayer player = new SoundPlayer ( Properties.Resources.Success );
player.Play ( );
}
this.Handles.Add ( handle );
}
}
}
我该如何改进它以减少时间?是不是我无法控制?这是一个论坛网站,如果我的程序完成较晚,那么它会延迟打开链接,例如在主题打开后 1-4 分钟,这在大多数情况下为时已晚,因为其他人已经在我之前采取了行动。
此外,我有 3 个链接,this.Links
每个链接在threadLinks
一页上包含大约 40 个主题,这是我正在阅读的内容,以查看是否打开了新主题。不确定通过这些 120 是否会有所作为。
有没有办法只获得页面的一小部分?