0

In my application, when a user clicks on a menu item, they are prompted to select a file which I then use a background worker to load and parse with progress being reported to a progress bar during this operation. when the work is complete, I start a new window which i pass the parsed data to. for some reason, the new window is getting opened before the data is parsed and null arguments are passed instead of the data.

my Click Event:

private void CIRCUIT_INSTALATION_SCHEDULED_Click(object sender, RoutedEventArgs e)
{
    //prevents users from selecting other menu items 
    disableAll();

    System.Windows.Forms.OpenFileDialog mainExcelFileDialog = new System.Windows.Forms.OpenFileDialog();
    mainExcelFileDialog.InitialDirectory = System.Configuration.ConfigurationManager.AppSettings["MainWorkbookDirectory"];
    mainExcelFileDialog.Title = "Main Excel File";
    mainExcelFileDialog.ShowDialog();
    string mainPath = mainExcelFileDialog.FileName;
    this.Cursor = System.Windows.Input.Cursors.Wait;
    BackgroundWorker cisWorker = new BackgroundWorker();

    cisWorker.DoWork += cisWorker_DoWork;
    cisWorker.ProgressChanged+=cisWorker_ProgressChanged;
    cisWorker.RunWorkerCompleted += cisWorker_RunWorkerCompleted;            
    cisWorker.RunWorkerAsync(mainPath);            
}

my Do Work:

void cisWorker_DoWork(object sender, DoWorkEventArgs e)
{            
    BackgroundWorker worker = sender as BackgroundWorker;
    using (FileStream fs = new FileStream(e.Argument.ToString() , FileMode.Open))
    {
        //copy filestream to memorystream chunk by chunk, reporting progress.
        using (MemoryStream ms = new MemoryStream())
        {
            byte[] buffer = new byte[32768];
            int read;
            int steps = Convert.ToInt32(fs.Length / buffer.Length);
            int i = 0;
            while ((read = fs.Read(buffer, 0, buffer.Length)) > 0)
            {

                ms.Write(buffer, 0, read);
                i++;
                var progress = new Decimal(i) / new Decimal(steps);
                var outOf = Math.Round(progress * 50);
                worker.ReportProgress(Convert.ToInt32(outOf));
            }

            worker.ReportProgress(50);
            ms.Position = 0;
            //load excel workbook dataset from memorystream
            using (var xlr = Excel.ExcelReaderFactory.CreateOpenXmlReader(ms))
            {
                xlr.IsFirstRowAsColumnNames = true;
                mainWorkbook = xlr.AsDataSet();
                worker.ReportProgress(100);

            }

        }
    }
}

my on complete:

void cisWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
    this.Cursor = System.Windows.Input.Cursors.Arrow;
    CircuitInstallationScheduledWindow CIS_Window = new CircuitInstallationScheduledWindow();
    CIS_Window.Show();
    CIS_Window.Closed += CIS_Window_Closed;
}

EDIT: I added the mainPath argument to the cisWorker.runWorkerAsync() method. the problem persists unfortunately.

H H
  • 263,252
  • 30
  • 330
  • 514
  • 1
    Based on the code provided, I don't see you passing any data to CIS_Window. Perhaps this is why it's null? If not, please show how the data is passed. – dmeglio Jul 24 '15 at 21:11
  • 1
    You are getting an exception in DoWork and you ignore it in Completed. See [this answer](http://stackoverflow.com/a/4807200/) for a canonic Completed event. – H H Jul 24 '15 at 21:15
  • no you are right. I have been changing my code so much trying to get this fixed last time I must have forgot to pass in the mainPath argument. I changed it though and it still starts too soon. I will edit the question. – Michael Billingham Jul 24 '15 at 21:17
  • If I am getting an exception in DoWork why doesn't it get thrown? I don't have any try/catches. How do i know what the exception was so I can handle it? – Michael Billingham Jul 24 '15 at 21:22
  • Side-note...you should be checking the result of `mainExcelFileDialog.ShowDialog();` for `OK`. If the user hits Cancel you'll be processing what filename?...and your UI will be disabled because you already called `disableAll();`! – Idle_Mind Jul 24 '15 at 23:07

2 Answers2

2

Basically you should check for your data not being null, i.e that no error occurred during DoWork

void cisWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
    if (e.Error == null)
    {
        /* Your mainWorkbook shouldn't be null */

        this.Cursor = System.Windows.Input.Cursors.Arrow;
        CircuitInstallationScheduledWindow CIS_Window = new CircuitInstallationScheduledWindow();
        CIS_Window.Show();
        CIS_Window.Closed += CIS_Window_Closed;
    }
    else
    {
        // Handle error
    }
} 
Arthur Rey
  • 2,990
  • 3
  • 19
  • 42
0

Henk Holterman was right. I was getting an error because I had not set cisWorker.WorkerReportsProgress = true; I still don't know why the error wasn't breaking the debugger since it wasn't being handled, but I did a print statement in my Completed method which helped me track down the culprit.

void cisWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
    {
        Console.WriteLine(e.Error);
        this.Cursor = System.Windows.Input.Cursors.Arrow;
        CircuitInstallationScheduledWindow CIS_Window = new CircuitInstallationScheduledWindow(mainWorkbook, abfsDatabase);
        CIS_Window.Show();
        CIS_Window.Closed += CIS_Window_Closed;
    }
Community
  • 1
  • 1