有没有办法等待" ReportProgress
方法结束?
我在ReportProgress
方法上更新了一个列表,在我使用该列表调用方法之后,这是错误的,除非ReportProgress
有完了。
#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
我无法上传整个方法,这是将节点添加到用户界面的部分,可以在此处看到从数据库更新:Look at the edit below
答案 0 :(得分:1)
如果要序列化对ReportProgress方法 AND 的访问权限,则无法更改worker方法,可以将ReportProgress更改为此...
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,但无论如何,结果在您的问题中是IMPLICIT。