hi friends: recently, I am writing a multi-thread program. but I meet a hard question for me. I used multi-thread to get web information. but, the main program halts when I click Cancel button. Code is below: <pre lang="cs">private void Cancel_bt_Click(object sender, EventArgs e) { //currntThreaArray is a array including all of threads if (this.currntThreaArray != null && this.currntThreaArray.Length > 0) { new Thread(new ThreadStart(() => { foreach (Thread t in this.currntThreaArray) { t.Abort(); // t.Join(30000); } this.JudgeIfThreadCanceled(); })).Start(); } }
public void JudgeIfThreadCanceled() { do { bool ifAllCancel = true; foreach (Thread t in this.currntThreaArray) { if ((t.ThreadState != ThreadState.AbortRequested) || (t.ThreadState != (ThreadState.WaitSleepJoin | ThreadState.AbortRequested))) { ifAllCancel = false; break; } } if (ifAllCancel) { this.WebLog_RB.Invoke((MethodInvoker)delegate() { this.WebLog_RB.AppendText("--- Cancel all thread !!! --\r\n"); }); } Thread.Sleep(1000); } while (true); }
public void Start(out Thread[] threadArray) { counter = 0; rowNumber = 0; // Use multi-thread to get web information // The firstly: Create a thread array threadArray = new Thread[this.threadNum]; // The second: Assign value to Thread Array for (int i = 0; i < threadArray.Length; i++) { threadArray[i] = new Thread(new ThreadStart(this.GetACompany)); threadArray[i].Name = "Thread" + i; threadArray[i].IsBackground = true; // false stand for current thread no end // threadArrayState is a collection to record thread is running or end this.threadArrayState.Add(threadArray[i].Name, false); } // The third: Begin to perform multi-thread for (int j = 0; j < threadArray.Length; j++) { threadArray[j].Start(); } }
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)