2

I am having problems in using progress bar while I he app reads and gets data from excel sheet

This is what I want to do-:

  • My application reads data from cells of various excel sheets in a workbook
  • Once the data is read, a message box is shown to user which reads "File read successfully"
  • I need to run a progress bar in the form while the data is being read from the file and it need to show % completion once before the message box pops up

This is what I did -:

  • I added a background worker and a progress bar
  • this is snippet of code which calls Run worker, RunWorkerAsync is called when the button is clicked and file to read is selected.

    // Start the BackgroundWorker.
            fileReadingbackgroundWorker.RunWorkerAsync();
    
  • A snippet that reports progress

    int percentProcessFile =1;
    for (int i = 1; i <= countSheets; i++)
    {
        readSheet = (Excel.Worksheet)excelWorkbook.Worksheets.get_Item(i);
    
        for (int row = 2; row <= 100; row++)
        {
            if (readSheet.Cells[1][row].Text.ToString() != "")
            {
                for (int column = 1; column <= 15; column++)
                {
                    String Key = readSheet.Cells[1][row].Text.ToString();
                    String readCaptionCell = readSheet.Cells[column][1].Text.ToString();
                    String readCurrentCell = readSheet.Cells[column][row].Text.ToString();
                    if (readSheet.Name == "ISMB")
                          ISMBSections.Add(Key, readCaptionCell, readCurrentCell);
                    else if (readSheet.Name == "ISMC")
                          ISMCSections.Add(Key, readCaptionCell, readCurrentCell);
                 }                        
           }
           else
           {
               break;
           }
           precentProgressFile++;
           fileReadingbackgroundWorker.ReportProgress(precentProgressFile);
       }   
    }
    

The line fileReadingbackgroundWorker.ReportProgress(precentProgressFile); throws an exception

  • I have added the following lines as well

    private void fileReadingbackgroundWorker_DoWork(object sender, DoWorkEventArgs e)
    {
        for (int i = 1; i <= 100; i++)
        {
            // Wait 100 milliseconds.
            Thread.Sleep(100);
            // Report progress.
            fileReadingbackgroundWorker.ReportProgress(j);
    
        }
    }
    
    private void fileReadingbackgroundWorker_ProgressChanged(object sender, ProgressChangedEventArgs e)
    {
        fileReadingProgressBar.Value = e.ProgressPercentage;
    }
    

The line fileReadingbackgroundWorker.ReportProgress(precentProgressFile); in the second snippet throws an exception

I am unable to understand what goes wrong here. I am using Visual Studio 12.0 and .Net 4.0 Please ask if you have any questions

Not a duplicate of Accessing UI Control from BackgroundWorker Thread - C# reason for this is I have not used DispatcherInvoke anywhere.

Community
  • 1
  • 1
vin
  • 869
  • 4
  • 17
  • 33
  • Please add the message of the exception. Exceptions are messages to the developer, what exactly went wrong. Your program is trying to tell you, but you don't listen (yet). – nvoigt Nov 10 '13 at 13:33
  • possible duplicate of [Accessing UI Control from BackgroundWorker Thread - C#](http://stackoverflow.com/questions/4428817/accessing-ui-control-from-backgroundworker-thread-c-sharp) – Abdusalam Ben Haj Nov 10 '13 at 14:00
  • Not a duplicate of Accessing UI Control from BackgroundWorker Thread - C# reason for this is I have not used DispatcherInvoke anywhere. – vin Nov 11 '13 at 11:04

1 Answers1

0

The BackgroundWorker has a property called WorkerReportsProgress which needs to be set to true in order to report the progress.

therealtbs
  • 162
  • 10