[摘要] 当程序运行时间很长,然后避免程序假死,比较好的办法是增加一个进度条,如果当前只有你的这一个功能在跑,用Dispatcher类是一个简单的好
当程序运行时间很长,然后避免程序假死,比较好的办法是增加一个进度条,如果当前只有你的这一个功能在跑,用Dispatcher类是一个简单的好选择。
这里用wpf的窗口来实现。
public partial class ProgressBarView : Window { ExternalCommandData m_commandData=null; StrightRigidFrameBridgeModel m_model =null; Element m_element = null; ProgressModel m_ProgressModel; bool applicationCancle =false; public ProgressBarView(ExternalCommandData commandData, StrightRigidFrameBridgeModel model,Element elem) { InitializeComponent(); m_commandData = commandData; m_model = model; m_element = elem; ProgressModel pModel = new ProgressModel(); m_ProgressModel = pModel; this.DataContext = pModel; applicationCancle = false; } public void StrightRigidFrameBridgeCreater(ExternalCommandData commandData, StrightRigidFrameBridgeModel model, Element elem) { using (TransactionGroup transGroup = new TransactionGroup(commandData.Application.ActiveUIDocument.Document,"Creat Rebar")) { transGroup.Start("Creat Stright Rigid Frame Bridge"); ICollection<ElementId> elementIds = new List<ElementId>() { elem.Id }; for (int i = 0; i < (model.ListBoxGriderModel.Count - 1); i++) { m_ProgressModel.ProgressInfomation ="剩下"+ (model.ListBoxGriderModel.Count - 1 - i-1).ToString() +"个节段"; m_ProgressModel.ProgressValue= double.Parse(i.ToString())/ (model.ListBoxGriderModel.Count - 2) * 100; System.Windows.Forms.Application.DoEvents(); BoxGriderModel boxGriderModelI = model.ListBoxGriderModel[i]; BoxGriderModel boxGriderModelJ = model.ListBoxGriderModel[i + 1]; #region 将表格的族参数赋予给对应的族参数 #endregion Transaction transaction1 = new Transaction(commandData.Application.ActiveUIDocument.Document); transaction1.Start("改变族参数"); elem.LookupParameter("iH").Set(MillimetersToUnits(boxGriderModelI.XLG)); //变截面箱梁i截面的高度 elem.LookupParameter("iHI5").Set(MillimetersToUnits(boxGriderModelI.DBH)); //变截面箱梁i截面底板的高度 elem.LookupParameter("iT").Set(MillimetersToUnits(boxGriderModelI.FBH)); //变截面箱梁i截面腹板的宽度 elem.LookupParameter("iHI1").Set(MillimetersToUnits(boxGriderModelI.TBH)); //变截面箱梁i截面顶板的高度 elem.LookupParameter("jH").Set(MillimetersToUnits(boxGriderModelJ.XLG)); //变截面箱梁i截面的高度 elem.LookupParameter("jHI5").Set(MillimetersToUnits(boxGriderModelJ.DBH)); //变截面箱梁i截面底板的高度 elem.LookupParameter("jT").Set(MillimetersToUnits(boxGriderModelJ.FBH)); //变截面箱梁i截面腹板的宽度 elem.LookupParameter("jHI1").Set(MillimetersToUnits(boxGriderModelJ.TBH)); //变截面箱梁i截面顶板的高度 elem.LookupParameter("BeamLength").Set(MillimetersToUnits(boxGriderModelJ.JDCD)); //变截面箱梁节段长度 elem.LookupParameter("detZ").Set(MillimetersToUnits(boxGriderModelI.SJGC) - MillimetersToUnits(boxGriderModelJ.SJGC)); //变截面箱梁两个z截面的高差 elem.LookupParameter("detY").Set(MillimetersToUnits(boxGriderModelI.Y_Coordinate) - MillimetersToUnits(boxGriderModelJ.Y_Coordinate)); //变截面箱梁两个z截面的高差 transaction1.Commit(); #region 将改变参数后的族放到相应位置 Transaction transaction2 = new Transaction(commandData.Application.ActiveUIDocument.Document); transaction2.Start("将族复制到相应位置"); XYZ point = new XYZ(MillimetersToUnits(boxGriderModelI.X_Coordinate), MillimetersToUnits(boxGriderModelI.Y_Coordinate), MillimetersToUnits(boxGriderModelI.SJGC)); LocationPoint familyInstancePoint = elem.Location as LocationPoint; XYZ tanslationVector = point.Subtract(familyInstancePoint.Point); ElementTransformUtils.CopyElements(commandData.Application.ActiveUIDocument.Document, elementIds, tanslationVector); transaction2.Commit(); #endregion if (applicationCancle == true) { break; } } if(applicationCancle==true) { transGroup.RollBack(); this.Close(); } else { transGroup.Assimilate(); } } } private double MillimetersToUnits(double value) { return UnitUtils.ConvertToInternalUnits(value, DisplayUnitType.DUT_MILLIMETERS); } private void window_Loaded(object sender, RoutedEventArgs e) { Thread thread = new Thread(excute); thread.Start(); } private void excute() { this.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Normal, (ThreadStart)delegate () { StrightRigidFrameBridgeCreater(m_commandData, m_model, m_element); }); } private void cancelAsyncButton_Click(object sender, RoutedEventArgs e) { this.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Normal, (ThreadStart)delegate () { applicationCancle = true;}); } }}
以上代码通过System.Windows.Forms.Application.DoEvents()来不断让程序在线程和主程序中来回切换,程序边计算一部分,然后进度条更新进度。
使用Dispartcher类注意用Try catch,并及时关掉线程。
注意上述代码会产生点问题,你会发现在程序跑的时候,构件是一个个生成的,这样给人感觉很奇怪。你需要将上述代码优化一下,将整个生成过程放在一个transaction里面。特别是有闲置程序在跑的时候,这种做法更安全,因为你在doevent的时候,很可能会让闲置程序也跟着跑,这明显会导致错误。因为老代码不见了,我懒得改,就用几句话代替。