Click here to Skip to main content
15,881,687 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
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-
VB
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        lockfield()
        txt_fname.Focus()
        btn_updt.Enabled = False
        DateTimePicker1.Enabled = False
        Dim dt As New DataTable
        Try
            con.ConnectionString = str
            'con.Open()
            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")
        Finally
            'con.Close()
        End Try
    End Sub


this shows data in the fields-
VB
Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
       clearfields()
       ds.Clear()
       txt_add.DataBindings.Clear()
       txt_email.DataBindings.Clear()
       txt_fname.DataBindings.Clear()
       txt_lname.DataBindings.Clear()
       txt_m1.DataBindings.Clear()
       txt_m2.DataBindings.Clear()
       txt_mname.DataBindings.Clear()
       txt_notes.DataBindings.Clear()
       txt_ph1.DataBindings.Clear()
       txt_ph2.DataBindings.Clear()
       DateTimePicker1.DataBindings.Clear()

       Try
           con.ConnectionString = str
           'con.Open()
           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, "stud.email")
           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
           MsgBox(ex.Message)
       Finally
           'con.Close()
       End Try

       ' Binding Contact Image to PictureBox

       Try
           con.ConnectionString = str
           con.Open()
           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
           'MsgBox(ex.Message)
       Finally
           con.Close()
       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?


Thanks.
Posted
Updated 8-Mar-13 21:04pm
v2
Comments
[no name] 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!
[no name] 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!
ali_heidari_ 8-Mar-13 10:08am    
give more details to know which part of your application is reason of this issue .

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.
 
Share this answer
 
v2
if you need initialize controls without hanging the software use a thread .
a simple thread :
C#
ThreadStart ts = new ThreadStart(Initialier); // initializer is that method wich must intialize your controls
Thread t1 = new Thread(ts);
t1.Start();

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
backgroundworker
progressbar

hope help!
good luck!
 
Share this answer
 
v2

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