SynchronizationContext.Post在BackgroundWorker中不起作用

时间:2011-12-22 20:14:48

标签: c# multithreading synchronization backgroundworker synchronizationcontext

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

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

The program MVST.CodeSync.exe version 2.0.0.0 stopped interacting with Windows and was closed.
To see if more information about the problem is available, check the problem history in the Action Center control panel.
 Process ID: 1f5c
 Start Time: 01ccc0e2e7ca1d42
 Termination Time: 16
 Application Path: C:\Users\ganders\Desktop\NewCodeSync\MVST.CodeSync.exe
 Report 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);
}

1 个答案:

答案 0 :(得分:2)

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

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