进度条不启动,直到窗口未关闭才进入每个循环

本文关键字:循环 窗口 启动 | 更新日期: 2023-09-27 18:34:04

我正在开发一个导出插件,将Revit模型导出到数据库。

为此,我的主窗口正在调用ExportEngine,这是一个窗口:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Autodesk.Revit.UI;
using Autodesk.Revit.DB;
using UserConnect;
using System.Collections.ObjectModel;
using System.IO;
namespace UserConnect
{
    public partial class ExportEngine : Window
    {
        private List<Component> components = new List<Component>();
        private static String zoneName = "REVIT";
        private User user;
        private Server neo4j = new Server("172.16.1.104", 8000);
        public ExportEngine(User userp)
        {
            user = userp;
            InitializeComponent();
        }
        public void addComponent(Component c)
        {
            components.Add(c);
        }
        public bool save()
        {
            this.ShowDialog();
            float progress = components.Count / 100;
            
            foreach (Component c in components)
            {
               //Here is my export request, working good
               pbStatus.Value+=progress;
            }
            this.Close();
            return result;
        } 
    }
}

我在 Main 中称之为:

db = new ExportEngine(user);
foreach(String name in mats)
{
    Component c = new Component(name, "m2",1);
    db.addComponent(c);
}
db.save();

我不明白为什么我的 save() 仅在我关闭进度条窗口后才开始,而这个进度条根本没有移动(没有进度)。

进度条不启动,直到窗口未关闭才进入每个循环

save

窗口关闭之前不会运行

ShowDialog在您显示的窗口关闭之前不会返回。这就解释了为什么它直到窗口关闭才运行。

进度条不更新

试试这个。

pbStatus.MaxValue = components.Count; //make sure you set the max value
foreach (Component c in components)
{
    pbStatus.Value+= 1; //increment by one item as you have processed on item.
}