1

I am developing a WPF, C# application and I have to read a very big CSV file and write to a database. This process takes a long time so I want to at least show a progress bar that grows in size as it nears completition.

Now, I have the following code at the top:

private readonly BackgroundWorker worker = new BackgroundWorker();

Then inside the loop I have this:

worker.WorkerReportsProgress = true;
worker.ReportProgress((100 * i) / 10000);

and I have a private sub like this:

private void worker_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
    prgBar.Value = Math.Min(e.ProgressPercentage, 100);
}

Yet I don't see the progress bar updating or anything happening, program still hangs. Am I doing something wrong?

UPDATE: Tried the MSDN guide here http://msdn.microsoft.com/en-us/library/cc221403%28v=vs.95%29.aspx and I somehow failed to get it working with that example. I adjusted the looping part of course. Heh. It still stays idle and nothing updates. Heck, all I want to do is get a small counter that increases every time a line is read and added.

UPDATED CODE:

    private BackgroundWorker bw = new BackgroundWorker();

    public Process()
    {


        bw.WorkerReportsProgress = true;
        bw.WorkerSupportsCancellation = true;
        bw.DoWork += new DoWorkEventHandler(bw_DoWork);
        bw.ProgressChanged += new ProgressChangedEventHandler(bw_ProgressChanged);
        bw.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bw_RunWorkerCompleted);
    }


    private void bw_DoWork(object sender, DoWorkEventArgs e)
    {
        BackgroundWorker worker = sender as BackgroundWorker;


                // Perform a time consuming operation and report progress.



                int d = 0;
                Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();



                // Set filter for file extension and default file extension 
                dlg.DefaultExt = ".csv";
                dlg.Filter = "CSV File (*.csv)|*.csv";
                dlg.Title = "file";


                // Display OpenFileDialog by calling ShowDialog method 
                Nullable<bool> result = dlg.ShowDialog();
                if (result == true)
                {
                    // Open document 
                    string filename = dlg.FileName;
                    List<String[]> fileContent = new List<string[]>();

                    using (FileStream reader = File.OpenRead(@filename)) // mind the encoding - UTF8
                    using (TextFieldParser parser = new TextFieldParser(reader))
                    {

                        using (SqlConnection conn = new SqlConnection(Classes.PublicVariables.Connection))
                        {

                            parser.Delimiters = new[] { "," };
                            parser.HasFieldsEnclosedInQuotes = true;
                            while (!parser.EndOfData)
                            {

                                    string[] line = parser.ReadFields();
                                    fileContent.Add(line);

                                    SqlCommand comm = QUERYANDPARAMETERS

                                    d += 1;
                                    comm.ExecuteNonQuery();
                                    worker.ReportProgress((d * 10));


                        }
                    }

                }


        }


           private void bw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
    {
lblCount.Content = "Complete";
    }
    private void bw_ProgressChanged(object sender, ProgressChangedEventArgs e)
    {
        this.lblCount.Content = (e.ProgressPercentage.ToString() + "%");
    }

I am getting an excerption when the worker runs.

 An exception of type 'System.NullReferenceException' occurred inSolution.exe but was not handled in user code

Additional information: Object reference not set to an instance of an object.

UPDATE3

Got it working! All I needed was the same code as above but this:

        if (bw.IsBusy != true)
        {
            bw.RunWorkerAsync();
        }
        else
        {
            bw_DoWork(null, null);
        }

In the event handler for the button press.

6
  • 1
    mathematically speaking, (100 * i) / 10000 is the same as i / 100 Feb 28, 2014 at 2:29
  • I just wanted to fill something there, changed to 9999 then :P. Still no go
    – Lord Relix
    Feb 28, 2014 at 2:30
  • Have you looked here: stackoverflow.com/questions/854548/… ?
    – Tony
    Feb 28, 2014 at 2:31
  • Ok, let me see if I can get it working with DoWork. My first time working with this
    – Lord Relix
    Feb 28, 2014 at 2:35
  • 2
    Why are you calling bw_DoWork if your back ground worker is busy? This defeats the purpose doesn't it?
    – tsells
    Feb 28, 2014 at 3:35

2 Answers 2

3

This will not compile as-is, but should get you started in the right direction:

private readonly BackgroundWorker worker
    = new BackgroundWorker { WorkerReportsProgress = true };

public MainWindow()
{
    InitializeComponent();

    worker.DoWork += worker_DoWork;
    worker.ProgressChanged += worker_ProgressChanged;
}

private void worker_DoWork(object sender, DoWorkEventArgs doWorkEventArgs)
{
    // Do some long process, break it up into a loop so you can periodically
    //  call worker.ReportProgress()

    worker.ReportProgress(i);  // Pass back some meaningful value
}

private void worker_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
    prgBar.Value = Math.Min(e.ProgressPercentage, 100);
}
0
0

is the varible i int? if it is, the value of (100 * i) / 10000 will return 0 while i < 100. and the param of ReportProgress method should be a precent value. you can change it to worker.ReportProgress(i);,try it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.