我有这个棘手的任务,我一直试图安静地完成,但直到现在我想不出任何办法让它工作。无论如何,这是场景......
我有一个 winform 应用程序包含一个列表视图和一个按钮。listview 包含 1 列,其中包含我需要稍后传递给我的函数的数据。该列包含可以说包含链接列表的 50 行。
现在我有了这个函数,我用它来获取和抓取这些链接的内容(一次 5 个链接),使用并行多线程模式(任务并行库):
//List<int> currentWorkingItem //contains the indices of the items in listview
//List<string> URLsList //contains the URLs of the items in listview
Parallel.ForEach(URLsList, new ParallelOptions() { MaxDegreeOfParallelism = 5 }, (url, i, j) =>
{
//show to user this link is currently being downloaded by highlighting the item to green...
this.BeginInvoke((Action)(delegate()
{
//current working item
mylistview.Items[currentWorkingItem[(int)j]].BackColor = green;
}));
//here I download the contents of every link in the list...
string HtmlResponse = GetPageResponse(url);
//do further processing....
});
现在上面的代码完美运行......但有时我希望用户中止当前正在运行的某个线程并继续列表中的其余线程......这可以实现吗?如果是这样,请帮帮我..我真的很感激任何解决方案或建议..