C#后台工作人员等待报告进度
有没有办法“等待”ReportProgress方法结束 我正在更新C#后台工作人员等待报告进度,c#,multithreading,backgroundworker,C#,Multithreading,Backgroundworker,有没有办法“等待”ReportProgress方法结束 我正在更新ReportProgress方法上的一个列表,在调用该方法后,我正在使用该列表,除非ReportProgress完成,否则该列表是错误的 #region addNodesToUi //Dispatcher.CurrentDispatcher.BeginInvoke((Action)(() => )); nodes = new List<NodeViewMode
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
#区域添加节点用户界面
//Dispatcher.CurrentDispatcher.BeginInvoke((操作)(()=>);
节点=新列表();
尝试
{
//Dispatcher.CurrentDispatcher.BeginInvoke((操作)(()=>);
WriteLine(“成功创建新节点设备模型”);
}
捕获(例外情况除外)
{
logger.addMessage(“创建新节点设备模型时出错”+ex.Message+“内部:”+ex.InnerException.Message);
Debug.WriteLine(“创建新节点设备模型时出错”+ex.Message+“内部:+ex.InnerException.Message”);
}
整数宽度=0;
整数高度=0;
int i=0;
ObservableCollection allSystems=新的ObservableCollection();
锁(mainNetLocker)
{
allSystems=MainNet.Systems;
}
foreach(allSystems.ToList()中的var系统)
{
Debug.WriteLine(“系统中的Foreach内部:“+system.Name+”interface“+system.Interfaces.Count+”structs“+system.structs.Count”);
如果(节点==null)
Debug.WriteLine(“fuuuckkk!”);
尝试
{
Debug.WriteLine(“添加节点之前”);
尝试
{
Debug.WriteLine(“正在尝试添加:”+system.Name+“”+system.InputNum+“”+system.OutputNum+“”+system.Interfaces.Count+“”+system.Enums.Count+“”+system.Structs.Count);
//Dispatcher.CurrentDispatcher.BeginInvoke((操作)(()=>nodes.Add(CreateNode(system.Name,新点(宽度,高度),false,system.InputNum,system.OutputNum,system.Interfaces,system.Enum,system.Structs,update));
添加(CreateNode(system.Name,新点(宽度,高度),false,system.InputNum,system.OutputNum,system.Interfaces,system.Enums,system.Structs,update));
//bw.ReportProgress(i++,tempn
}
捕获(例外情况除外)
{
logger.addMessage(“将新节点添加到列表中时出错”+ex.Message+“内部:+ex.InnerException.Message”);
Debug.WriteLine(“将新节点添加到列表中时出错”+ex.Message+“Inner:+ex.InnerException.Message”);
}
WriteLine(“成功将新节点添加到列表”);
}
捕获(例外情况除外)
{
logger.addMessage(“将新节点添加到列表中时出错”+ex.Message+“内部:+ex.InnerException.Message”);
Debug.WriteLine(“将新节点添加到列表中时出错”+ex.Message+“Inner:+ex.InnerException.Message”);
}
宽度+=150;
如果(宽度>=700)
{
宽度=0;
高度+=100;
}
}
如果(MainWindow.IsFlow)
{
Object[]getInterfacesWithGuidToFlowParam=新对象[1];
getInterfacesWithGuidToFlowParam[0]=MainWindow.GuidToFlow;
尝试
{
interfacesForFlow=(列表)getInterfacesWithGuidToFlow.Invoke(sqlDB,getInterfacesWithGuidToFlowParam);
Debug.WriteLine(“Success getInterfacesWithGuidToFlow”+interfacesForFlow.Count);
}
捕获(例外情况除外)
{
logger.addMessage(“getInterfacesWithGuidToFlow中的错误:+ex.Message+”内部:+ex.InnerException.Message”);
Debug.WriteLine(“getInterfacesWithGuidToFlow中的错误:+ex.Message+”内部:+ex.InnerException.Message”);
}
}
foreach(allSystems.ToList()中的var系统)
{
if(system.OutputNum>0)//此系统有一个输出连接器
{
//int i=0;
foreach(system.Outputs中的var outId)//循环抛出当前系统连接到的所有系统ID
{
Debug.WriteLine(“out id=“+outId”);
ConnectionViewModel连接=空;
尝试
{
//Dispatcher.CurrentDispatcher.BeginInvoke((操作)(()=>connection=new ConnectionViewModel());
连接=新连接视图模型();
编写(“成功创建新的ConnectionViewModel”);
}
捕获(例外情况除外)
{
logger.addMessage(“创建新连接ViewModel时出错”+ex.Message+“内部:+ex.InnerException.Message”);
Debug.WriteLine(“创建新连接ViewModel时出错”+ex.Message+“Inner:+ex.InnerException.Message”);
}
对象[]getSystemNameParams=新对象[1];
getSystemNameParams[0]=outId;
字符串destSystemName=“”;
尝试
{
destSystemName=(字符串)getSystemName.Invoke(sqlDB,getSystemNameParams);
}
捕获(例外情况除外)
{
logger.addMessage(“getSystemName中的错误:+ex.Message+”内部:+ex.Inne
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();
}
}