Click here to Skip to main content
15,892,005 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
I have a windows form application which is using a database on remote.

It takes to much time to connect, so I want to show a progressbar untill it's connecting to server. How can I do this?

I have a class for opening the connection. Here it is:

C#
public void openconn()
   {
       if (con == null)

        con = new SqlConnection("Data Source=69.10.00.000;Initial Catalog=property;user ID=******; password= ******");
       if (con.State == ConnectionState.Closed)
           try
           {
               con.Open();
           }
           catch
           {
               System.Windows.Forms.MessageBox.Show("Server not found!! please check your internet connection!");
           }

   }
Posted
Updated 20-Mar-12 7:40am
v3

Run this code in a Background thread and display the progress bar before calling then remove it after return. There is a considerable amount if info available on background threads here and on the web.

Some additional pointers.

You should not hard code the connection string. Use the connectionString element in app.config file, that's what it is for.

Don't use Exception handling for program flow such as how you are displaying a message box. If you are going to use exception handling then use it, an empty catch does nothing but decrease the performance of you app.
 
Share this answer
 
Comments
fjdiewornncalwe 20-Mar-12 12:43pm    
+5. Sound Advice.
Hello

Try to show the progress bar in a different thread.

You must use delegate and control's InvokeRequired.

http://msdn.microsoft.com/en-us/library/system.windows.forms.control.invokerequired.aspx[^]


Or use BackgroundWorker Class

look at this link:

http://msdn.microsoft.com/en-us/library/system.componentmodel.backgroundworker.aspx[^]
 
Share this answer
 
v2
BackgroundWorker is the easiest. The following code shows how this is done:
C#
public MainWindow()
{
    InitializeComponent();

    var bw = new BackgroundWorker();
    bw.DoWork += new DoWorkEventHandler(bw_DoWork);
    bw.WorkerSupportsCancellation = true;
    bw.WorkerReportsProgress = true;
    bw.ProgressChanged += new ProgressChangedEventHandler(bw_ProgressChanged);
    bw.RunWorkerAsync();

}

void bw_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
    //Update UI here
    var newText = e.UserState;
}

void bw_DoWork(object sender, DoWorkEventArgs e)
{
    var bw = (BackgroundWorker) sender;
    while (!bw.CancellationPending)
    {
        //Do RS-232 stuff here
        var newText = "Put new information here";
        bw.ReportProgress(1, newText);
    }
}



Your problem will be is providing progress. The only good idea I can tell you is to only a few of the records at a time. So get the count of records, and then do an appropriate Skip and Take, reporting progress change in the end. The other option is to guess how long it will take, and have another BackgoundWorker that will report progress.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900