0

What i want to achieve :

  • Step 1: When the form loads, I wanted to display a Loading Form (as dialog) which contains a Progress Bar and a Progress Label.

  • Step 2: While this is happening, I want to run a BackgroundWorker to retrieve the records from a database and fill my DataTable. The BackgroundWorker will update the Loading Form of the stages it has completed.

  • Step 3: When all records has been retrieved, close the Loading Form and display the records in the DataGridView

Things to note :

  • The DataGridView is bound to my DataSet.[Table].

  • The Loading Form is a Windows Form not a MessageBox.

  • I'm using Visual Studio 2017 Community Edition.

  • I plan to use this implementation for all my database interactions such as Update Etc.

Issues :

  • How can update the Progress Bar & Progress Label control on the Loading Form from bgwTransactionListing_ProgressChange

  • My bgwTransactionListing_DoWork is really not doing any work.

Code

Here is the code for Form_Load

namespace MyApplication
{
     public partial class frmTransactionListing : Form
     {
          public frmTransactionListing()
          {
              InitializeComponent();
          }

          frmload LoadingForm = new frmload();

          private void frmTransactionListing_Load(object sender, EventArgs e)
          {
              bgwTransactionListing.RunWorkerAsync();
          }

Here is my code for BacgroundWorker_DoWork:

private void bgwTransactionListing_DoWork(object sender, DoWorkEventArgs e)
    {
        try
        {
            bgwTransactionListing.ReportProgress(0, "Loading Records");
            System.Threading.Thread.Sleep(100);
            bgwTransactionListing.ReportProgress(100, "Completed");
        }
        catch (Exception ex)
        {
            MessageBox.Show("We have encountered an error and cannot proceed. Error: " + ex.Message);
        }
    }

Here is my code for BacgroundWorker_ProgressChanged:

private void bgwTransactionListing_ProgressChanged(object sender, ProgressChangedEventArgs e)
    {            
        if(e.ProgressPercentage == 0)
        {
            LoadingForm.ShowDialog();
        }
    }

Here is my code for BackgroundWorker_RunWorkerCompleted. This is where I am trying to close the Loading Form:

private void bgwTransactionListing_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
    {
        dgTransactionList.Refresh();
    }

Advanced Thanks to all your help.

UPDATE 1: I disabled the loading form and tried putting the Table Adapter Fill method on RunWorkerCompleted and the DataGridView is now displaying the records. I thought the Fill Method is the process of retrieving records from my database. Apparently its not.

UPDATE 2: I have updated the codes with what I currently have after i applied the suggestions from @russelrillema @Jimi. Now, I am able to show and close the Loading From. I have also updated my issues.

rainbasa
  • 69
  • 8
  • Not the cleanest approach but if this is the route you are going can you not store the frmload globally. Initialise it in the frmTransactionListing_Load and then you'll have access to it in the bgwTransactionListing_RunWorkerCompleted where you can close it? – russelrillema Jan 12 '19 at 06:28
  • I stored it globally because im planning to use it for all my other database interactions. – rainbasa Jan 12 '19 at 06:39
  • Based on your samples LoadingForm is being created in frmTransactionListing_Load so it's not currently global. What I mean is put frmload LoadingForm; outside of frmTransactionListing_Load and then inside the frmTransactionListing_Load method put LoadingForm = new frmload(); Then you will have access to LoadingForm inside all methods in the frmTransactionListing class – russelrillema Jan 12 '19 at 06:45
  • After `LoadingForm.ShowDialog();`, the code stops there. It will continue with `bgwTransactionListing.RunWorkerAsync();` only after you have closed `LoadingForm`. – Jimi Jan 12 '19 at 06:49
  • Thank you @russelrillema for your inputs. I will try your suggestions. – rainbasa Jan 12 '19 at 06:51
  • Thank you @Jimi for your inputs. I will try your suggestions. – rainbasa Jan 12 '19 at 06:51
  • I didn't actually suggest anything. I'm saying that you can't show a modal Form *and* excute the code that comes after at the same time. The current code will not continue until the modal Form is closed. You could opt for a progress animation, [something like this](https://stackoverflow.com/a/39142535/7444103), since you're fill a `TableAdapter`, which doesn't provide means to show a `ProgressBar`. Or simply show the standard waiting cursor. I wouldn't waste too much time implementing something like this. Maybe when you upgrade/update the application (unless you're already there :). – Jimi Jan 12 '19 at 07:07
  • Or you could show the second Form using just `LoadingForm.Show(this);`. – Jimi Jan 12 '19 at 07:15
  • I have a sample for a WPF app [here](https://github.com/SirRufo/WpfWithSplash/blob/master/WpfWithSplash/App.xaml.cs) - you may find it useful – Sir Rufo Jan 12 '19 at 07:56

0 Answers0