내가 추가 : 플래그 모두 생성자에서 false로 설정되어backgroundworker/recursive 루프를 중지/재개 할 수 있습니까? Form1에의 상단에서
bool pause;
bool resume;
:
System.Threading.ManualResetEvent _busy = new System.Threading.ManualResetEvent(true);
는 또한 Form1의 상단에 두 개의 플래그를 추가했다. 내가이 추가 BackgroundWorker 구성 DoWork 이벤트에서
private List<string> test(string url, int levels,DoWorkEventArgs eve)
{
if (pause == true)
{
_busy.Reset();
}
if (resume == true)
{
_busy.Set();
}
this.Invoke(new MethodInvoker(delegate { label3.Text = label3.Text = (Int32.Parse(label12.Text) + Int32.Parse(label10.Text)).ToString(); }));
HtmlWeb hw = new HtmlWeb();
List<string> webSites;
List<string> csFiles = new List<string>();
csFiles.Add("temp string to know that something is happening in level = " + levels.ToString());
csFiles.Add("current site name in this level is : " + url);
try
{
this.Invoke(new MethodInvoker(delegate { ColorText.Texts(richTextBox1, "Loading The Url: " , Color.Red); }));
this.Invoke(new MethodInvoker(delegate { ColorText.Texts(richTextBox1, url + "...",Color.Blue); }));
HtmlAgilityPack.HtmlDocument doc = TimeOut.getHtmlDocumentWebClient(url, false, "", 0, "", "");
this.Invoke(new MethodInvoker(delegate { ColorText.Texts(richTextBox1, " Done " + Environment.NewLine, Color.Red); }));
currentCrawlingSite.Add(url);
webSites = getLinks(doc);
removeDupes(webSites);
removeDuplicates(webSites, currentCrawlingSite);
removeDuplicates(webSites, sitesToCrawl);
if (removeExt == true)
{
for (int i = 0; i < webSites.Count; i++)
{
webSites.Remove(removeExternals(webSites));
}
}
if (downLoadImages == true)
{
webContent.retrieveImages(url); }
if (levels > 0)
sitesToCrawl.AddRange(webSites
this.Invoke(new MethodInvoker(delegate { label7.Text = sitesToCrawl.Count.ToString(); }));
this.Invoke(new MethodInvoker(delegate { label12.Text = currentCrawlingSite.Count.ToString(); }));
if (levels == 0)
{
return csFiles;
}
else
{
for (int i = 0; i < webSites.Count(); i++)//&& i < 20; i++) {
string t = webSites[i];
if ((t.StartsWith("http://") == true) || (t.StartsWith("https://") == true)) // replace this with future FilterJunkLinks function
{
csFiles.AddRange(test(t, levels - 1, eve));
}
}
return csFiles;
}
}
catch
{
failedUrls++;
this.Invoke(new MethodInvoker(delegate { label10.Text = failedUrls.ToString(); }));
this.Invoke(new MethodInvoker(delegate { ColorText.Texts(richTextBox1, " Failed " + Environment.NewLine, Color.Green); }));
return csFiles;
}
}
: 다음
내가 재귀 루프 기능을 가지고 그리고 내가 이력서에 대한 일시 정지 하나의 이벤트 하나를 클릭하여 두 개의 버튼을 가지고
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
_busy.WaitOne();
this.Invoke(new MethodInvoker(delegate { label2.Text = "Website To Crawl: "; }));
this.Invoke(new MethodInvoker(delegate { label4.Text = mainUrl; }));
test(mainUrl, levelsToCrawl, e);
}
:
private void button4_Click(object sender, EventArgs e)
{
pause = true;
}
private void button5_Click(object sender, EventArgs e)
{
resume = true;
}
버튼을 클릭하면 일시 중지가 다시 시작됩니다. 전자 단추. 과정은 계속 진행 중입니다. _busy.Reset();을 만들기 위해 각 버튼에 플래그가없는 상태에서 시도했습니다. 및 _busy.Set(); 하지만 아무것도 아니야.
내가 원하는 것은 어떻게 든 재귀 루프를 일시 중지하고 다시 시작하는 것입니다. 임원이 backgroundworker에 연결되어 있는지 여부는 확실하지 않지만 아이디어는 프로세스를 일시 중지하고 다시 시작하는 것입니다.
어떻게해야합니까?
감사합니다.
다음 링크를 확인하십시오. http://social.msdn.microsoft.com/Forums/en-US/netfxbcl/thread/48305e65-cea6-4a88-8818-c122e152daa8/ – Boomer
왜 (재귀) 루프가 멈추거나 일시 중지됩니까? ? Wait()가 보이지 않습니다. –
BackgroundWorker를 사용하고 있으므로 this.Invoke 대신 ProgressChanged를 사용해야합니다. 혼란을 막으려면 버튼이 _busy 이벤트를 제어하고 일시 중지를 제거하고 bool을 다시 시작해야합니다. _busy 작업을하려면 _busy.WaitOne() 메서드를 테스트 메서드 내부에 호출해야합니다. – Casperah