0

I want to update progress bar after a method which is present in another class is called. In my case am reading data from a excel and storing it in datatable for further operations as the excel file is large it takes some time. I want to update the progress of reading and stroing data on UI.

Following is my UI(xaml) code:

<Window x:Class="WpfTutorialSamples.Misc_controls.ProgressBarIndeterminateSample"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="ProgressBarIndeterminateSample" Height="100" Width="300">
    <Grid Margin="20">
         <button click="btn_submit_click">Run</button>
        <ProgressBar Minimum="0" Maximum="100" Name="pbStatus" IsIndeterminate="True" />
    </Grid>
</Window>

Following is my xaml.cs code:

namespace progress_bar
{
    public partial class MainWindow : Window 
    {
        public static string path = @"c:\sample.xlsx";
        public MainWindow()
        {
            InitializeComponent();
        }

        private void btn_submit_click(object sender, RoutedEventArgs e)
        {
          Read_file  R1 = new Read_file();
          var DT = R1.read(path);
        }
    }

    class Read_file
    {
       public DataTable dt = new DataTable("dt"); // creating datatable
       string Name = string.Empty;
       dt.Columns.Add("Name", typeof(String)); // adding "Name" column to datatable

        public DataTable read(string path)
        {
          //Performed some operation such as opening, etc
          //Applied Autofilter on excel file as I have to select specific data from excel file
          //filtered_data is Excel Range of Filtered data          

          ///using this loop to iterate through filtered data and store it Datatable 
          foreach(Excel.Range area in filtered_data.Areas)
          {
            foreach(Excel.Range row in area.Rows)
             {
                   int index = row.Row // selecting row index
                  ////select each row from filtered data and saving it to Datatable. 
                 Name = Convert.ToString((xlsheet.cells[index,1] as Excel.Range).Text);  
                 df.Rows.Add(Name);
                  //// I want to display the progress of selectig and saving data to UI  
             }
          }
  
        }
    }      
}

I want to update progress bar according to the execution in foreach loop inside Read method. On internet I read about the BackgroundWorker but I am very confused about where and how to use BackgroundWorker methods. I am still new to c# and wpf.

NiK8
  • 19
  • 6
  • _"I am very confused"_ is not a useful problem statement. See duplicate for a general example of a WPF implementation that uses `ProgressBar` and `BackgroundWorker`. That said, you have a number of different challenges to solve, so work the problem one piece at a time. Make sure you understand how `BackgroundWorker` or suitable alternative works, and can successfully use it with `ProgressBar` before you start worrying about integrating that with the Excel-related logic. ... – Peter Duniho Dec 30 '20 at 23:03
  • ... Note: Excel interop may have a requirement that the Excel object be created and used in a STA thread, which barring additional work on your part will mean it has to be used in the UI thread, which may limit the usefulness of asynchronous/background processing techniques like `BackgroundWorker`. – Peter Duniho Dec 30 '20 at 23:03

0 Answers0