Help in implementing an Asynchronous Progress Bar WPF C #

0

Good afternoon, I have an application where I run a long and slow process and would like to implement a progress bar that shows the percentage of the process that has already been executed, however if I implement the progress bar on the same screen the process is running , the screen hangs and the progress bar is not populated ... I'm using the MVVM standard and implemented a "Progress bar Class" class to use whenever I need it, so I wanted to run it by clicking on the button that calls the long process method

This is my method that has the long process (Implemented in the viewmodel):

        public void GeneratePackage(PackingConfigFile pf)
    {
        int total;
        total = 0;           
        foreach(BasicVariable bv in pf.basicVariableList.bvList)
        {
            total += bv.bvData.Count;
        }
        pb = new ProgressBarClass(total);
        pb.Show();
        int bvListSize = pf.basicVariableList.bvList.Count;
        var status = HDFql.Execute(
            "CREATE FILE pmdfq.h5;" +
            "USE FILE pmdfq.h5;" +
            "CREATE GROUP informations;" +
            "CREATE GROUP datasets");
        HDFql.Execute("SHOW USE FILE");
        HDFql.CursorFirst();
        var cursor = HDFql.CursorGetChar();
        var erro = HDFql.ErrorGetMessage();
        foreach (BasicVariable bv in pf.basicVariableList.bvList)
        {
            status = HDFql.Execute("CREATE DATASET datasets/" + bv.bvTag + " as VARCHAR(" + bv.bvData.Count + ",4)");
            foreach (OPC_UA opc in bv.bvData)
            {
                var x = bv.bvData.IndexOf(opc);
                status = HDFql.Execute("INSERT INTO datasets/" + bv.bvTag + "( " + bv.bvData.IndexOf(opc) + "::: ) VALUES (" + opc.GetHashCode() + "," + opc.timeStamp.ToString("yyyy-MM-dd") + ", " + opc.quality.ToString() + ", " + opc.data + ");");
                //HDFql.Execute("SELECT FROM  datasets/"+bv.bvTag);
                HDFql.CursorFirst();
                erro = HDFql.ErrorGetMessage();
                HDFql.CursorFirst();
                cursor = HDFql.CursorGetChar();
                pb.IncrementProgress();
            }
        }
    }

Where the step pb.IncrementProgress () increments the progressbar Value attribute that is already initialized with the maximum and minimum value

This is the progressbar window class I created:

    public partial class ProgressBarClass : Window
{
    public ProgressBarClass(int max,int min = 0)
    {
        InitializeComponent();
        ProgressBar.Minimum = min;
        ProgressBar.Maximum = max;
        ProgressBar.Value = 0;
    }

    public void IncrementProgress()
    {
        ProgressBar.Value++;
    }

    private void Window_ContentRendered(object sender, EventArgs e)
    {
        BackgroundWorker worker = new BackgroundWorker();
        worker.WorkerReportsProgress = true;
        worker.DoWork += worker_DoWork;
        worker.ProgressChanged += worker_ProgressChanged;

        worker.RunWorkerAsync();
    }
    void worker_DoWork(object sender, DoWorkEventArgs e)
    {
        var percent = ProgressBar.Value / ProgressBar.Maximum * 100;
        (sender as BackgroundWorker).ReportProgress(Convert.ToInt32(percent));
    }

    void worker_ProgressChanged(object sender, ProgressChangedEventArgs e)
    {
        ProgressBar.Value = e.ProgressPercentage;
    }
}

And here is where the method is called in the view:

        private void Button_Click(object sender, RoutedEventArgs e)
{
    pakVM.GeneratePackage(pakVM.packingConfigFile);

}
    
asked by anonymous 07.06.2018 / 18:03

0 answers