0

I have a Form where one can Sign In and it could take a while till the data gets loaded into the Form. So I wanted to create a seperate Form (loadScreen.cs) with a Progress Bar when the Form is loading. I tried this in the loadScreen.cs Form:

 private void Form1_Load(object sender, EventArgs e)
 {
   worker = new BackgroundWorker();
   worker.WorkerReportsProgress = true;
   worker.WorkerSupportsCancellation = true;

   worker.DoWork += new DoWorkEventHandler(worker_DoWork);
   worker.ProgressChanged +=
          new ProgressChangedEventHandler(worker_ProgressChanged);
   worker.RunWorkerCompleted +=
         new RunWorkerCompletedEventHandler(worker_RunWorkerCompleted);
 }



 void worker_DoWork(object sender, DoWorkEventArgs e)
 {
   int percentFinished = (int)e.Argument;
   while (!worker.CancellationPending && percentFinished < 100)
 {
   percentFinished++;
   worker.ReportProgress(percentFinished);
   System.Threading.Thread.Sleep(50);
 }
 e.Result = percentFinished;
 }


 void worker_ProgressChanged(object sender, ProgressChangedEventArgs e)
 {
   progressBar1.Value = e.ProgressPercentage;
 }


 void worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
 {
  this.Close();
  }

I've read that the worker_DoWork method should have the code which takes longer to load. I don't know how to handle this since my button is in Form1. When it's clicked then I go to another class with

  private void signIn_Click(object sender, EventArgs e)
    {
        var logIn = new LogIn(this);
        logIn.checkUserInput(this);
    }

and there I execute the operations which load certain things. How to connect everything? I need help!

Takeda15
  • 55
  • 2
  • 10
  • Because I wanted to put the whole operations into another class for runtime reasons. It's the inner structure I have in my project – Takeda15 Mar 17 '15 at 08:14
  • 1
    maybe you looking for a splashscreen? https://www.google.at/webhp?sourceid=chrome-instant&ion=1&espv=2&ie=UTF-8#q=c%23+splashscreen – Mat Mar 17 '15 at 08:15
  • hm. Maybe that would be a solution too. That's nearly the same isn't it? – Takeda15 Mar 17 '15 at 08:24

1 Answers1

1

I'm actually in the process of creating a general-purpose dialogue for this sort of thing. It's not going to be ready in time to be of use to you but I would suggest that you go along similar lines. Create your "Loading" dialogue so that it accepts a delegate and invokes it in the DoWork event handler. The main form can then contain a method that does the work and you can pass a delegate for that method to the dialogue. I'll post a very basic example.

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private DataTable table;

    private void button1_Click(object sender, EventArgs e)
    {
        var work = new Action(GetData);

        using (var f2 = new Form2(work))
        {
            f2.ShowDialog();
            this.dataGridView1.DataSource = this.table;
        }
    }

    private void GetData()
    {
        this.table = new DataTable();

        using (var adapter = new SqlDataAdapter("SELECT * FROM MyTable", "connectionstring here"))
        {
            adapter.Fill(table);
        }
    }
}


public partial class Form2 : Form
{
    private Action work;

    public Form2(Action work)
    {
        InitializeComponent();

        this.work = work;
    }

    private void Form2_Load(object sender, EventArgs e)
    {
        this.backgroundWorker1.RunWorkerAsync();
    }

    private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
    {
        this.work();
    }

    private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
    {
        this.DialogResult = DialogResult.OK;
    }
}

Note that there's no real way to measure progress when using a data adapter so you could only really display a marquee progress bar in this case.

jmcilhinney
  • 50,448
  • 5
  • 26
  • 46
  • Example added. It's simple so I haven't commented it but just ask if you need some extra explanation. – jmcilhinney Mar 17 '15 at 08:26
  • I don't really know what the Sql Adapter is in this code. Is this just an example for a method that is called? – Takeda15 Mar 17 '15 at 08:39
  • @Takeda15, that method could do anything. In this case it's querying a SQL Server database and populating a `DataTable` but it could do whatever you want. It is being executed on a secondary thread though, so it mustn't actually touch the UI; background work only. – jmcilhinney Mar 17 '15 at 09:23
  • Ok. What's the Form2? – Takeda15 Mar 17 '15 at 10:23
  • @Takeda15, what did I say? "Create your "Loading" dialogue so that it accepts a delegate and invokes it in the DoWork event handler". `Form2` is the only place in that code that there's a `DoWork` event handler so what do you think it is? – jmcilhinney Mar 17 '15 at 12:15