Click here to Skip to main content
11,926,429 members (60,098 online)
Rate this:
Please Sign up or sign in to vote.
See more: C# VB SQL-Server VB.NET
Hello friends,
I have developed one application which works perfect but takes 10-15 seconds to load. this is much compare to application.
how do i make it to load fast and i want to show progress i it loads. and i want to load all the controls in the background.

I have 10 textbox's, 1 combobox, 1 picturebox, 6 butttons.
following is the code-
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        btn_updt.Enabled = False
        DateTimePicker1.Enabled = False
        Dim dt As New DataTable
            con.ConnectionString = str
            Dim adp1 As New SqlDataAdapter("select rtrim(fname)+' '+rtrim(mname)+' '+rtrim(lname) as [name] from stud", con)
            adp1.Fill(ds1, "stud")
            dt = ds1.Tables("stud")
            Me.ComboBox1.DataSource = dt
            Me.ComboBox1.DisplayMember = "name"
            Me.ComboBox1.SelectedIndex = 0
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Critical, "My Friends")
        End Try
    End Sub

this shows data in the fields-
Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
           con.ConnectionString = str
           adp = New SqlDataAdapter("select address,bdate,email,fname,lname,mname,mobile1,mobile2,phone1,phone2,note from stud where rtrim(fname)+' '+rtrim(mname)+' '+rtrim(lname)='" & ComboBox1.Text & "'", con)
           adp.Fill(ds, "stud")
           dt1 = ds.Tables("stud")
           txt_add.DataBindings.Add("Text", ds, "stud.address")
           txt_email.DataBindings.Add("Text", ds, "")
           txt_fname.DataBindings.Add("Text", ds, "stud.fname")
           txt_lname.DataBindings.Add("Text", ds, "stud.lname")
           txt_m1.DataBindings.Add("Text", ds, "stud.mobile1")
           txt_m2.DataBindings.Add("Text", ds, "stud.mobile2")
           txt_mname.DataBindings.Add("Text", ds, "stud.mname")
           txt_notes.DataBindings.Add("Text", ds, "stud.note")
           txt_ph1.DataBindings.Add("Text", ds, "stud.phone1")
           txt_ph2.DataBindings.Add("Text", ds, "stud.phone2")
           DateTimePicker1.DataBindings.Add("Text", ds, "stud.bdate")
       Catch ex As Exception
       End Try
       ' Binding Contact Image to PictureBox

           con.ConnectionString = str
           Dim cmd As New SqlCommand("select photo from stud where id=(select id from stud where rtrim(fname)+' '+rtrim(mname)+' '+rtrim(lname)='" & ComboBox1.Text & "')", con)
           PictureBox1.Image = Image.FromStream(New IO.MemoryStream(CType(cmd.ExecuteScalar, Byte())))
       Catch ex As Exception
       End Try
   End Sub

When the form loads all controls shows scattered and after 5-10 seconds the're loaded. what i want to do is show loading progress to user and and in the background load the form.
Can you guys tell me how can i do this with background worker/ multithreading?

Posted 8-Mar-13 4:36am
Edited 8-Mar-13 22:04pm
Shameel 8-Mar-13 9:46am
How do you expect anyone to solve your problem without seeing your code? As a general guideline, time your code and find out which lines take longer to execute.
javedsmart 9-Mar-13 5:36am
Question updated!
ThePhantomUpvoter 8-Mar-13 9:56am
If that length of time really bothers you then profile your code, use a splash screen or only do the bare minimum on the main thread that gets your application going.
javedsmart 9-Mar-13 5:36am
Question updated!
source.compiler 8-Mar-13 10:08am
give more details to know which part of your application is reason of this issue .
Too vague to answer. It is not really related to multithreading. What are you going to do in another thread if nothing is loaded? If you do have such parallel work, what? That the question. As we don't have any detail, there is no a solution.
javedsmart 9-Mar-13 5:36am
Question updated!
As to the database, all database work should be in a separate thread. Do you have problems with that?
source.compiler 9-Mar-13 3:43am
so you want the controls initializing be in a seperate thread ,yea? and maybe you want show initializig operation with a progressbar! right?
javedsmart 9-Mar-13 5:35am
Yes. can you help?
source.compiler 9-Mar-13 6:02am
i hope !
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

Generally speaking you need to use separate threads for the loading. I don't know what your application does during these long seconds, but whatever is done, can be done in the background. During that time you can display a Splash screen (static or animated one), so the end user will not feel like it takes too long to load. This splash screen can be shown at start.
Since you haven't provided any details, I'll also assume another scenario: Applications with a huge size (like 5MB), takes a long time to load. There is a way to solve that as well. You need to create a small "loader" application which will only display your Splash screen, and initiate the loading of the large portion of the application. This method is used for CDs and DVDs, where the autorun.inf file leads to this "loader" so when the CD / DVD is inserted, the Splash screen is displayed, while the software is loaded.

In order to display the progress, you need to create a WM_TIMER event, every 0.5 seconds, and have the a way to measure the progress of the large application loading. That can be done in several ways, but again, it depends on the reason for taking such time to load. Every TIMER event, you promote the progress bar in a portion that is equal to the entire time required divided by the time that elapsed.
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

if you need initialize controls without hanging the software use a thread .
a simple thread :
ThreadStart ts = new ThreadStart(Initialier); // initializer is that method wich must intialize your controls
Thread t1 = new Thread(ts);
but this ,just prevent hanging ! but you have user waiting issue !
so some component like backgroundworker and progressbar control ,can be useful to show an animation until your thread done work! so check this links :
How to: Use a Background Worker
BackgroundWorker Class

hope help!
good luck!

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

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.151126.1 | Last Updated 9 Mar 2013
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100