0

I wish I could represent graphically connecting to my remote database, since it is not possible to know the exact connection duration, I thought that an endless progress control should do the job. Currently I am using WaitCursor which doesn't give me satisfaction.

When I use controls such as Progressbar, When I click to begin connection the progress is stopped since it is not used in a separate tread, so I tried to use the backgroundWorker, but have no idea on how to use it to database connection purpose.

My code:

using System;
using System.Linq;
using System.Windows.Forms;

namespace MyNameSpace
{
    public partial class Form1 : Form
    {
        dbDataContext db;
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            myProgressBar.Style = ProgressBarStyle.Marquee;
            System.Windows.Forms.Application.DoEvents();
            db = new dbDataContext();

            var Users = from p in db.Users
                        where p.UserName == TxtUser.Text
                        select p;
            foreach (var record in Users)
            {
                Global._UserName = record.UserName;
                Global._UserID = record.ID;
            }
            label1.Text = "User ID = " + Global._UserID;
        }
    }
}
Sami-L
  • 5,553
  • 18
  • 59
  • 87
  • 1
    Endless progress controls suck. Just my two cents. Why don't you display a dancing unicorn instead? Better yet, just tell the user "This might take a minute or two." – Robert Harvey Jul 18 '12 at 15:25
  • @Robert, Have you a dancing unicorn to rent :-) – Sami-L Jul 18 '12 at 15:31
  • @AlphaBird, Surely this method executes so quickly that there is hardly a need for progress indication? How many users do you have? – KingCronus Jul 18 '12 at 20:08
  • @KingCronus, Currently I have no idea on the number of users since this is resquested for a new project. – Sami-L Jul 18 '12 at 21:34

1 Answers1

3

If you don't know the length of time it will take, the usual technique is to display the Progress control in marquee mode.

myProgressBar.Style = ProgressBarStyle.Marquee;
Robert Harvey
  • 178,213
  • 47
  • 333
  • 501
KingCronus
  • 4,509
  • 1
  • 24
  • 49
  • 1
    See http://stackoverflow.com/questions/312936/windows-forms-progressbar-easiest-way-to-start-stop-marquee – Robert Harvey Jul 18 '12 at 15:33
  • Thank you KingCronus, I could run a progress control in marqee mode, now I need to ensure that it will run while connecting to database... – Sami-L Jul 18 '12 at 15:43
  • Erm... It should run as long as you don't dismiss it. – Robert Harvey Jul 18 '12 at 15:44
  • I tried it, **it doesn't run** untill database connection completed – Sami-L Jul 18 '12 at 15:56
  • Are you sure you are running in the background? If you are on the UI thread it may not animate until the calling method finishes execution. – KingCronus Jul 18 '12 at 15:58
  • Please how can I run it in the background ? – Sami-L Jul 18 '12 at 16:00
  • Alternatively, if you want a cheap and nasty way to test it, put a call to "Application.DoEvents()" in immediately after you set it to Marquee. – KingCronus Jul 18 '12 at 16:05
  • myProgressBar.Style = ProgressBarStyle.Marquee; System.Windows.Forms.Application.DoEvents(); db = new dbDataContext(); also animates after connection completion. – Sami-L Jul 18 '12 at 16:22
  • *Sigh*, where exactly are you calling the code? Can you edit the original question with some code? Otherwise I can't really help you further. – KingCronus Jul 18 '12 at 16:42