2010-09-07 54 views
0

我正在一個單獨的線程中加載一個大樹視圖。此線程從窗體的加載事件開始。如何檢查表單仍然'活着'

一切順利,直到加載事件發生錯誤。發生錯誤時,我關閉表單,加載我的樹視圖的線程必須中止。但我不知道該怎麼做。

問題是,窗體關閉,線程仍在工作,所以我得到一個InvalidOperationException。該計劃分解和線程的這部分強調:

tvQuestionnaire.Invoke((MethodInvoker)delegate 
{ 
    tvQuestionnaire.Nodes.Add(catNode); 
}); 

我的形式樹形視圖被稱爲tvQuestionnaire。整個功能(在我的後臺工作者中調用)如下所示:

private void SetTreeviewData() 
{ 
    // Get all categories 
    List<Category> categories = _questionnaire.GetCategoriesFromQuestionnaire(); 

    // Get all questions which are retrieved by the question manager 
    OrderedDictionary all_ordered_questions = _questionManager.AllQuestions; 

    // Store all the questions in a List<T> 
    List<Question> all_questions = new List<Question>(); 
    foreach (DictionaryEntry de in all_ordered_questions) 
    { 
     Question q = de.Value as Question; 
     all_questions.Add(q); 
    } 

    foreach (Category category in categories) 
    { 
     // Create category node 
     TreeNode catNode = new TreeNode(); 
     catNode.Text = category.Description; 
     catNode.Tag = category; 
     catNode.Name = category.Id.ToString(); 

     // Get all questions which belongs to the category 
     List<Question> questions = all_questions.FindAll(q => q.CategoryId == category.Id); 

     // Default set the font to bold (Windows issue) 
     Font font = new Font(tvQuestionnaire.Font, FontStyle.Regular); 

     foreach (Question question in questions) 
     { 
      // Create question node 
      TreeNode queNode = new TreeNode(); 
      queNode.Text = question.Question; 
      queNode.Tag = question; 
      queNode.Name = "Q" + question.Id; 
      queNode.NodeFont = font; 

      // Determine which treenode icon to show 
      SetTreeNodeIcon(ref queNode, question); 

      // Add node to category node 
      catNode.Nodes.Add(queNode); 
     } 

     if (_closing) 
      return; 

     // Add category node to treeview 
     tvQuestionnaire.Invoke((MethodInvoker)delegate 
     { 
      tvQuestionnaire.Nodes.Add(catNode); 

      // Now the category (and thus the questions) are added to treeview 
      // Set questions treenode icon 
      //SetTreeNodeIcon(questions); 
     }); 
    } 

    // Set each category under its parent 
    for (int i = tvQuestionnaire.Nodes.Count - 1; i >= 0; i--) 
    { 
     Category category = tvQuestionnaire.Nodes[i].Tag as Category; 
     TreeNode node = tvQuestionnaire.Nodes[i]; 

     if (IsWindow(this.Handle.ToInt32()) == 0) 
      return; 

     tvQuestionnaire.Invoke((MethodInvoker)delegate 
     { 
      if (category.ParentId == null) 
       return; 
      else 
      { 
       // Find parent node 
       TreeNode[] parentNodes = tvQuestionnaire.Nodes.Find(category.ParentId.ToString(), true); 

       //Remove current node from treeview 
       tvQuestionnaire.Nodes.Remove(node); 
       parentNodes[0].Nodes.Insert(0, node); 
      } 
     }); 
    } 
} 

這是我的後臺工作人員調用的唯一方法。

所以我的問題是,我怎樣才能防止發生異常?如何檢查樹形視圖所在的表單,仍然「活着」?

回答

2

當您需要關閉表單時,一種解決方案是調用Backgroundworker(BGW)的CancelAsync方法。在DoWork事件處理程序中,在循環的開始處檢查是否未請求取消。如果是,退出循環(和DoWork處理程序)。 在表單中,等待BGW完成(成功或取消)

0

爲什麼不抓住這個事件,而是放棄線程的執行?

0

檢查IsHandleCreated表單的屬性。如果表格仍然存在,那將是真的。

相關問題