首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >C# backgroundWorker wait

C# backgroundWorker wait
EN

Stack Overflow用户
提问于 2014-05-08 18:56:06
回答 1查看 1.2K关注 0票数 0

是否有任何方法“等待”ReportProgress方法结束?

我正在更新ReportProgress方法上的一个列表,在调用该方法之后,我将使用该列表,这是错误的,除非ReportProgress已经完成。

代码语言:javascript
运行
复制
#region addNodesToUi

             //Dispatcher.CurrentDispatcher.BeginInvoke((Action)(() => ));
                nodes = new List<NodeViewModel>();

            try
            {
                //Dispatcher.CurrentDispatcher.BeginInvoke((Action)(() => ));

                Debug.WriteLine("Success creating new nodeViewModel");
            }
            catch (Exception ex)
            {
                logger.addMessage("Error in creating new nodeViewModel " + ex.Message + " Inner: " + ex.InnerException.Message);
                Debug.WriteLine("Error in creating new nodeViewModel " + ex.Message + " Inner: " + ex.InnerException.Message);
            }
            int width = 0;
            int height = 0;
            int i = 0;
            ObservableCollection<Common.Model.System> allSystems = new ObservableCollection<Common.Model.System>();
            lock (mainNetLocker)
            {
                allSystems = MainNet.Systems;
            }

            foreach (var system in allSystems.ToList())
            {
                Debug.WriteLine("inside Foreach in system: " + system.Name + " interface " + system.Interfaces.Count + " structs " + system.Structs.Count);
                if (nodes == null)
                    Debug.WriteLine("FUUUCCKKKK!");
                try
                {
                    Debug.WriteLine("Before add node");
                    try
                    {
                        Debug.WriteLine("trying to add: " + system.Name + " " + system.InputNum + " " + system.OutputNum + " " + system.Interfaces.Count + " " + system.Enums.Count + " " + system.Structs.Count);
                        //Dispatcher.CurrentDispatcher.BeginInvoke((Action)(() => nodes.Add(CreateNode(system.Name, new Point(width, height), false, system.InputNum, system.OutputNum, system.Interfaces, system.Enums, system.Structs, update))));
                        nodes.Add(CreateNode(system.Name, new Point(width, height), false, system.InputNum, system.OutputNum, system.Interfaces, system.Enums, system.Structs, update));
                        //bw.ReportProgress(i++, tempn

                    }

                    catch (Exception ex)
                    {
                        logger.addMessage("Error adding new node to list " + ex.Message + " Inner: " + ex.InnerException.Message);
                        Debug.WriteLine("Error adding new node to list " + ex.Message + " Inner: " + ex.InnerException.Message);
                    }
                    Debug.WriteLine("Success adding new node to list");
                }
                catch (Exception ex)
                {
                    logger.addMessage("Error in adding new node to list " + ex.Message + " Inner: " + ex.InnerException.Message);
                    Debug.WriteLine("Error in adding new node to list " + ex.Message + " Inner: " + ex.InnerException.Message);
                }
                width += 150;
                if (width >= 700)
                {
                    width = 0;
                    height += 100;
                }
            }

            if (MainWindow.IsFlow)
            {
                Object[] getInterfacesWithGuidToFlowParam = new Object[1];
                getInterfacesWithGuidToFlowParam[0] = MainWindow.GuidToFlow;

                    try
                    {
                        interfacesForFlow = (List<String>)getInterfacesWithGuidToFlow.Invoke(sqlDB, getInterfacesWithGuidToFlowParam);

                        Debug.WriteLine("Success getInterfacesWithGuidToFlow " + interfacesForFlow.Count);
                    }
                    catch (Exception ex)
                    {
                        logger.addMessage("Error in getInterfacesWithGuidToFlow : " + ex.Message + " Inner: " + ex.InnerException.Message);
                        Debug.WriteLine("Error in getInterfacesWithGuidToFlow : " + ex.Message + " Inner: " + ex.InnerException.Message);
                    }

            }

            foreach (var system in allSystems.ToList())
            {
                if (system.OutputNum > 0)       //this system has an output connector
                {
                    //int i = 0;
                    foreach (var outId in system.Outputs)       //loop throw all systems ids that current system is connected to 
                    {
                        Debug.WriteLine("out id = " + outId);
                        ConnectionViewModel connection = null;
                        try
                        {
                            //Dispatcher.CurrentDispatcher.BeginInvoke((Action)(() => connection = new ConnectionViewModel()));
                            connection = new ConnectionViewModel();

                            Debug.Write("Success creating new ConnectionViewModel");
                        }
                        catch (Exception ex)
                        {
                            logger.addMessage("Error in creating new ConnectionViewModel " + ex.Message + " Inner: " + ex.InnerException.Message);
                            Debug.WriteLine("Error in creating new ConnectionViewModel " + ex.Message + " Inner: " + ex.InnerException.Message);
                        }
                        Object[] getSystemNameParams = new Object[1];
                        getSystemNameParams[0] = outId;
                        string destSystemName = "";
                        try
                        {
                            destSystemName = (String)getSystemName.Invoke(sqlDB, getSystemNameParams);
                        }
                        catch (Exception ex)
                        {
                            logger.addMessage("Error in getSystemName: " + ex.Message + " Inner: " + ex.InnerException.Message);
                            Debug.WriteLine("Error in getSystemName: " + ex.Message + " Inner: " + ex.InnerException.Message);
                        }

                        NodeViewModel sourceItem = null;
                        NodeViewModel destItem = null;
                        //Dispatcher.CurrentDispatcher.BeginInvoke((Action)(() => sourceItem = nodes.Find(x => x.Name == system.Name)));
                        lock (networkLocker)
                        {
                            sourceItem = nodes.Find(x => x.Name == system.Name);
                        }

                        //int sourceId = nodes.FindIndex(sourceItem);
                        Debug.Write("Success creating new sourceItem");
                        //Dispatcher.CurrentDispatcher.BeginInvoke((Action)(() => destItem = nodes.Find(x => x.Name == destSystemName)));
                        lock (networkLocker)
                        {
                            destItem = nodes.Find(x => x.Name == destSystemName);
                        }

                        Debug.Write("Success creating new destItem");
                        try
                        {
                            //Dispatcher.CurrentDispatcher.BeginInvoke((Action)(() => destItem.InputSystems.Add(sourceItem.Name)));
                            destItem.InputSystems.Add(sourceItem.Name);

                        }
                        catch (Exception ex)
                        {
                            logger.addMessage("Error adding input system: " + ex.Message);
                            Debug.Write("Error adding input system: " + ex.Message);
                        }
                        try
                        {
                            //Dispatcher.CurrentDispatcher.BeginInvoke((Action)(() => sourceItem.OutputSystems.Add(destItem.Name)));
                            sourceItem.OutputSystems.Add(destItem.Name);

                        }
                        catch (Exception ex)
                        {
                            logger.addMessage("Error adding OutputSystems: " + ex.Message);
                            Debug.Write("Error adding OutputSystems: " + ex.Message);
                        }
                        Debug.Write("Success bah");
                        //int destId = nodes.FindIndex(destItem);


                        //Dispatcher.CurrentDispatcher.BeginInvoke((Action)(() => connection.SourceConnector = sourceItem.OutputConnectors[i++]));
                        connection.SourceConnector = sourceItem.OutputConnectors[i++];

                        Debug.Write("Success bah");
                        //Dispatcher.CurrentDispatcher.BeginInvoke((Action)(() => connection.DestConnector = destItem.InputConnectors[destItem.InputConnectors.Count - 1]));
                        connection.DestConnector = destItem.InputConnectors[destItem.InputConnectors.Count - 1];


                        Debug.Write("Success bah");

                        // Add the connection to the view-model.
                        //
                        //Dispatcher.CurrentDispatcher.BeginInvoke((Action)(() => connection.Type = ConnectionViewModel.ConnectorType.REGULAR));
                        connection.Type = ConnectionViewModel.ConnectorType.REGULAR;

                        //Dispatcher.CurrentDispatcher.BeginInvoke((Action)(() => this.Network.Connections.Add(connection)));


                        //Debug.Write("Success bah");
                        if (MainWindow.IsFlow)
                        {
                            Debug.WriteLine("inside IsFlow!");

                            foreach (var @interface in interfacesForFlow)
                            {
                                String[] systems = @interface.Split('_');
                                Debug.WriteLine("Flow from: " + systems[0] + " To " + systems[1]);
                                if (systems[0].Equals(sourceItem.Name) && systems[1].Equals(destItem.Name))
                                    connection.Type = ConnectionViewModel.ConnectorType.FLOW;
                                    //Dispatcher.CurrentDispatcher.BeginInvoke((Action)(() => connection.Type = ConnectionViewModel.ConnectorType.FLOW));

                            }                                 

                        }
                        lock (networkLocker)
                        {
                            bw.ReportProgress(2, connection);
                        }
                    }
                }
                Dispatcher.CurrentDispatcher.BeginInvoke((Action)(() => ww.Close()));

                //bw2.ReportProgress(100);
            }
            #endregio

嗯,我不能上传整个方法,这是添加节点到你的UI中的一部分,更新的数据库可以在这里看到:请看下面的编辑

EN

回答 1

Stack Overflow用户

发布于 2014-05-08 20:24:02

如果您想序列化对ReportProgress方法的访问,并且不能更改worker方法,则可以将ReportProgress更改为

代码语言:javascript
运行
复制
    private readonly Semaphore _throttle = new Semaphore(1,1);
    void bw_ProgressChanged(object sender, ProgressChangedEventArgs e)
    {
        try
        {
            _throttle.WaitOne();
            if (e.ProgressPercentage == 1)       //update nodes
            {
                this.Network.Nodes.Add((NodeViewModel)e.UserState);
            }
            else if (e.ProgressPercentage == 2)       //update connections
            {
                this.Network.Connections.Add((ConnectionViewModel)e.UserState);
            }
            else if (e.ProgressPercentage == 3)
            {
                this.Network.Connections.Clear();
                this.Network.Nodes.Clear();
            }
            else if (e.ProgressPercentage == 4)
            {
                MainNet.Systems.Add((Common.Model.System)e.UserState);
            }
        }
        finally
        {
            _throttle.Release();
        }
    }

这种方法为回调添加了一个节流,这将迫使调用者等待ReportProgress完成。只有一个调用者可以一次输入方法的主体,这正是您想要实现的。缺点是队列会阻塞UI,但这个结果仍然隐含在您的问题中。

The 信号文档在这里

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23550271

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档