Hi,

Dear Friends i m developing a Inventory System, and i want to show a progress bar until the data loaded from the database and displayed on the screen,

so for that time a progress bar should be displayed and show the progress of retrieving of data from database, and progress bar should be hide when all data loaded from the database and displayed in the grid.

Thanx in Advance Dear Buddies :-)

Thanx Dear Friend for the Quick Reply :-)

Yaar,

the link u have post is fi9... but it only tells us how to create it and how to start and stop it.., but it doesn't give me my answer i.e

It must be depend on the System, that the time the PC takes to load the data from the database, it must show the progress according to that...

and im very confused that, how can i do that... is there anyone who can solve the problem :(

Oh, I hope someone can help you with that. You need to implement some sort of callback from the database load provided your db library supports that, which I think it probably does, but I don't know. What tool are you using for DB?

For database read operations you're better off showing a "marquee" progress bar that just moves back and forth until the operation completes and doesn't give an exact process. In order to know how long a database operation will take you would need to query the DB to get the affected row count and you would have to take over creating the local cursor and importing the record in batches of, say, 1% of the total row count so you could report progress back to your UI. You would also need to do this in another thread so your database retrieval operations would not block the main thread.

The list goes on and on and on .... Your best bet: Retrieving data in another thread (see BackgroundWorker ) and not reporting progress but rather showing a screen that the system is working.

Most people understand when the program locks up for a fraction of a second that it is busy and wait. The more code you add the slower the application will become (although negligible) so in most DB cases it doesn't make sense to even report progress -- but it does make sense to keep the data retrieval off in another thread.

@DdoubleD

Dear i am using Microsoft Access for Database.
Well thanx for your Help :)

@sknake

Dear sknake, sorry dear i didn't understand what did u say :'(

can u give me any little bit of code so i can understand...

Yaar,

i want to do the same work as "When we copy any huge size files from one place to another on hard disk, at that time windows shows us the progress of copying files"

I want to do just like that... :-/

is there anything which can tells us that how much time will be taken by this process???

if we found that, then may be we can solve this problem... :S

Ok -- here you go. This is a start but you will want to expand on it.

Query Runner:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Threading;

namespace daniweb.progress
{
  public delegate void QueryFinished(DataTable dt);

  public static class QueryRunner
  {
    private struct QueryStuff
    {
      public string Query { get; set; }
      public QueryFinished qfDelegate { get; set; }
    }

    public static void RunQuery(string Query, QueryFinished qfDelegate)
    {
      QueryStuff stuff = new QueryStuff();
      stuff.Query = Query;
      stuff.qfDelegate = qfDelegate;

      Thread t = new Thread(Starter);
      t.Start(stuff);
      Thread.Sleep(100); //give it enough time to start up
    }
    private static void Starter(object StartInfo)
    {
      QueryStuff stuff = (QueryStuff)StartInfo;
      DataTable result = new DataTable();
      try
      {
        Console.WriteLine(stuff.Query); //this is the query you would execute
        Thread.Sleep(1000 * 10); //simulate a long running query

        //Your query would return a DataTable or DataSet. I'm simulating data since I dont
        //want to use a DB Connection as they're hard for examples.
        result.Columns.Add("Column1", typeof(string));
        result.Columns.Add("Column2", typeof(string));
        for (int i1 = 0; i1 < 10; i1++)
        {
          DataRow row = result.NewRow();
          row[0] = Guid.NewGuid().ToString();
          row[1] = Guid.NewGuid().ToString();
          result.Rows.Add(row);
        }
      }
      catch
      {
        //Do some error handling. This thread CANNOT crash -- it will be bad news :)
      }
      finally
      {
        stuff.qfDelegate(result);
      }
    }
  }
}

Main form (caller)

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace daniweb.progress
{
  public partial class frmMain : Form
  {
    private frmProgress _progress;

    public frmMain()
    {
      InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
      _progress = new frmProgress();
      QueryRunner.RunQuery("select * from some table in the database", QueryFinished);
      _progress.ShowDialog();
    }

    private void QueryFinished(DataTable dt)
    {
      if (dataGridView1.InvokeRequired)
      {
        this.Invoke(new MethodInvoker(
                        delegate()
                        {
                          QueryFinished(dt);
                        }));

      }
      else
      {
        if (_progress != null)
        {
          _progress.Close();
          _progress.Dispose();
        }
        this.dataGridView1.DataSource = dt;
      }
    }

  }
}

Ok -- here you go. This is a start but you will want to expand on it.

Query Runner:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Threading;

namespace daniweb.progress
{
  public delegate void QueryFinished(DataTable dt);

  public static class QueryRunner
  {
    private struct QueryStuff
    {
      public string Query { get; set; }
      public QueryFinished qfDelegate { get; set; }
    }

    public static void RunQuery(string Query, QueryFinished qfDelegate)
    {
      QueryStuff stuff = new QueryStuff();
      stuff.Query = Query;
      stuff.qfDelegate = qfDelegate;

      Thread t = new Thread(Starter);
      t.Start(stuff);
      Thread.Sleep(100); //give it enough time to start up
    }
    private static void Starter(object StartInfo)
    {
      QueryStuff stuff = (QueryStuff)StartInfo;
      DataTable result = new DataTable();
      try
      {
        Console.WriteLine(stuff.Query); //this is the query you would execute
        Thread.Sleep(1000 * 10); //simulate a long running query

        //Your query would return a DataTable or DataSet. I'm simulating data since I dont
        //want to use a DB Connection as they're hard for examples.
        result.Columns.Add("Column1", typeof(string));
        result.Columns.Add("Column2", typeof(string));
        for (int i1 = 0; i1 < 10; i1++)
        {
          DataRow row = result.NewRow();
          row[0] = Guid.NewGuid().ToString();
          row[1] = Guid.NewGuid().ToString();
          result.Rows.Add(row);
        }
      }
      catch
      {
        //Do some error handling. This thread CANNOT crash -- it will be bad news :)
      }
      finally
      {
        stuff.qfDelegate(result);
      }
    }
  }
}

Main form (caller)

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace daniweb.progress
{
  public partial class frmMain : Form
  {
    private frmProgress _progress;

    public frmMain()
    {
      InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
      _progress = new frmProgress();
      QueryRunner.RunQuery("select * from some table in the database", QueryFinished);
      _progress.ShowDialog();
    }

    private void QueryFinished(DataTable dt)
    {
      if (dataGridView1.InvokeRequired)
      {
        this.Invoke(new MethodInvoker(
                        delegate()
                        {
                          QueryFinished(dt);
                        }));

      }
      else
      {
        if (_progress != null)
        {
          _progress.Close();
          _progress.Dispose();
        }
        this.dataGridView1.DataSource = dt;
      }
    }

  }
}

one must agree that i started this convention of posting solutions as zip files along with codes. Come on guys just acknowledge the fact. I am the father of posting the entire ready to run solutions with the code excerpts from those solutions. am not i?

Comments
you're the daddy!
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication6
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private void timer1_Tick(object sender, EventArgs e)
        {
            if (progressBar1.Value <= progressBar1.Maximum)
            {
                progressBar1.Value += 1;
                textBox1.Text = Convert.ToString(progressBar1.Value);
            }
            if (progressBar1.Value >= progressBar1.Maximum)
            {
                timer1.Enabled = false;
            }
        }
        private void button1_Click(object sender, EventArgs e)
        {
            timer1.Enabled = true;
            timer2.Enabled = false;
            timer3.Enabled = false;
            timer4.Enabled = false;
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            timer1.Enabled = false;
            timer2.Enabled = false;
            timer3.Enabled = false;
            timer4.Enabled = false;
        }
        private void button2_Click(object sender, EventArgs e)
        {
            timer2.Enabled = true;
            timer1.Enabled = false;
            timer3.Enabled = false;
            timer4.Enabled = false;
        }

        private void timer2_Tick(object sender, EventArgs e)
        {
             if (progressBar1.Value > progressBar1.Minimum)
            {
                progressBar1.Value -= 1;
                textBox1.Text = Convert.ToString(progressBar1.Value);
            }

             if (progressBar1.Value == progressBar1.Minimum)
             {

                 timer2.Enabled = false;
             }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            timer3.Enabled = true;
            timer1.Enabled = false;
            timer2.Enabled = false;
            timer4.Enabled = false;
        }

        private void timer3_Tick(object sender, EventArgs e)
        {
            if (progressBar1.Value <= progressBar1.Maximum)
            {
                progressBar1.Value += 1;
                textBox1.Text = Convert.ToString(progressBar1.Value);
                timer3.Enabled = false;
            }

            if (progressBar1.Value >= progressBar1.Maximum)
            {
                timer3.Enabled = false;
            }
        }

        private void timer4_Tick(object sender, EventArgs e)
        {
            if (progressBar1.Value > progressBar1.Minimum )
            {
                progressBar1.Value -= 1;
                textBox1.Text = Convert.ToString(progressBar1.Value);
                timer4.Enabled = false;
            }

            if (progressBar1.Value == progressBar1.Minimum )
            {

                timer4.Enabled = false;
            }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            timer4.Enabled = true;
            timer1.Enabled = false;
            timer2.Enabled = false;
            timer3.Enabled = false;
        }
    }
}

Edited 3 Years Ago by pyTony: fixed formatting

Attachments PB.jpg 29.33 KB
This question has already been answered. Start a new discussion instead.