ProgressBar没有启动,直到窗口未关闭才进入foreach循环

时间:2014-12-11 10:43:30

标签: c# wpf

我使用导出插件将revit模型导出到数据库。

为此,我的主窗口调用ExportEngine,它是一个Window:

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()仅在我关闭进度条窗口后启动,而且这个进度条根本没有移动(没有进展)。

1 个答案:

答案 0 :(得分:0)

save在Windows关闭之前不会运行

在您显示的窗口关闭之前,

ShowDialog不会返回。这就解释了为什么它在Windows关闭之前不会运行。

进度条不会更新

试试这个。

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.
}