1

我正在使用后台工作人员来完成我的所有处理。在那里,我有很多地方可以写入“日志”文本框。所有这些工作都很好,但在后台工作人员的最后,最后一行,我还有一个SynchronizationContext不会触发的呼叫。为什么所有其他调用都有效,而不是最后一个?

我应该补充一点,应用程序只是“挂起”,甚至还有一个 EventLog 条目说:

程序 MVST.CodeSync.exe 版本 2.0.0.0 停止与 Windows 交互并被关闭。
要查看有关问题的更多信息是否可用,请查看操作中心控制面板中的问题历史记录。
 进程 ID:1f5c
 开始时间:01ccc0e2e7ca1d42
 终止时间:16
 应用程序路径:C:\Users\ganders\Desktop\NewCodeSync\MVST.CodeSync.exe
 报告 ID:629f3533-2cd6-11e1-9e15-005056b75254

这是DoWork方法(在调用时执行RunWorkerAsync):

private void bgw_StartCompare(object sender, DoWorkEventArgs e)
{
    OnWriteLogArgs args = null;
    CompareData compareData = e.Argument as CompareData;

    // We need to iterate through all of the nodes and if they are checked, continue
    foreach (TreeNode subSystemNode in compareData.TreeNodes)
    {
        if (!subSystemNode.Checked)
            continue;

        args = new OnWriteLogArgs(String.Format("-------------------------- Comparing sub-system: \"{0}\" with CompareType: \"{1}\" --------------------------", subSystemNode.Text, compareData.CompareType));
        syncContext.Post(delegate { OnWriteLog(args); }, null);

        // Each of these nodes should be a server, so continue
        foreach (TreeNode serverNode in subSystemNode.Nodes)
        {
            if (!serverNode.Checked)
                continue;

            args = new OnWriteLogArgs(String.Format("-------------------------- Comparing server: \"{0}\" with CompareType: \"{1}\" --------------------------", serverNode.Text, compareData.CompareType));
            syncContext.Post(delegate { OnWriteLog(args); }, null);

            // The "tag" contains the server information that we need to do the comparison
            CustomConfig.Server server = (CustomConfig.Server)serverNode.Tag;

            if (!compareData.DoneInitialCompare)
                CompareAll(compareData, server, string.Empty, server.CompareBasePath, serverNode, compareData.CompareType);
            else
                CompareAllByTreeNode(compareData, server, serverNode, compareData.CompareType);
        }
    }

    syncContext.Post(delegate { OnWriteLog(new OnWriteLogArgs("Finished the compare...")); }, null);

    RebuildTreeViewArgs rArgs = new RebuildTreeViewArgs(compareData.OnlyShowDifferences, compareData.TreeNodes);
    syncContext.Post(delegate { OnRebuildTreeView(rArgs); }, null);

    MessageBox.Show("It made it...");

    syncContext.Post(delegate { OnWriteLog(new OnWriteLogArgs("Finished calling the rebuild tree view method...")); }, null);
}

您会注意到,在foreach循环结束时,我写了说Finished the compare...,并且确实写了,但是下一个同步调用:

     syncContext.Post(delegate { OnRebuildTreeView(rArgs); }, null);

永远不会被处决。这是那个方法:

private void OnRebuildTreeView(RebuildTreeViewArgs args)
{
    syncContext.Post(delegate { OnWriteLog(new OnWriteLogArgs("Made it to the OnRebuildTreeView method...")); }, null);

    while (bgw.IsBusy)
    {
        syncContext.Post(delegate { OnWriteLog(new OnWriteLogArgs("Sleeping...")); }, null);
        Thread.Sleep(1000);
    }

    syncContext.Post(delegate { OnWriteLog(new OnWriteLogArgs("Starting the rebuild of the TreeView...")); }, null);

    TreeNode[] tn = args.NewStructure;

    tvSync.Nodes.Clear();

    foreach (TreeNode node in tn)
        tvSync.Nodes.Add(node);

    foreach (TreeNode node in tvSync.Nodes)
        FixCheckedAndUnCheckedNodes(node);

    ReloadTreeView(args.OnlyShowDifferences);
}
4

1 回答 1

2
MessageBox.Show("It made it...");

也可能导致问题。因为这是期待 UI 线程,而事实上,你在后台线程

于 2011-12-22T20:20:20.960 回答