繁体   English   中英

如何检查表格仍然“有效”

[英]How to check a form is still 'alive'

我在单独的线程中加载了一个大的treeview。 该线程从表单的加载事件开始。

一切顺利,直到在加载事件中发生错误。 发生错误时,我关闭表单,并且加载我的Treeview的线程必须中止。 但我不知道该怎么做。

问题是,该窗体已关闭并且线程仍在工作,所以我得到了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);
            }
        });
    }
}

这是我的后台工作者调用的唯一方法。

所以我的问题是,如何防止发生异常? 如何检查树状视图所在的表单仍然“有效”?

一种解决方案是在需要关闭表单时调用后台工作程序(BGW)的CancelAsync方法。 在DoWork事件处理程序中,在循环开始时检查是否未请求取消。 如果是这样,请退出循环(和DoWork处理程序)。 在表单中,等待BGW完成(成功或取消)

为什么不赶上该事件,而不是中止线程的执行?

检查窗体的IsHandleCreated属性。 如果表单仍然存在,它将为true。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM