Loading Form Objects Before showing

Need a Helping hand please (:

  • (3 Pages)
  • +
  • 1
  • 2
  • 3

30 Replies - 6049 Views - Last Post: 17 July 2010 - 11:58 AM Rate Topic: -----

#1 Rickster0  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 16
  • View blog
  • Posts: 236
  • Joined: 08-July 08

Loading Form Objects Before showing

Posted 15 July 2010 - 05:09 PM

In some programs i see that the splash screen is used to load objects before everything is displayed to improve performance.

So, what i am asking, is what should/could i do to achieve this. What i would like is to use my Splash screen to load up the objects on the next form, so that they are displayed much faster ( as i am experiencing some lag when the form is displayed).

Hopefully there is a way i can achieve this, perhaps using timers or a background worker im not 100% sure.

Thank you in advance.

This post has been edited by Rickster0: 15 July 2010 - 06:47 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Loading Form Objects Before showing

#2 Sergio Tapia  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1253
  • View blog
  • Posts: 4,168
  • Joined: 27-January 10

Re: Loading Form Objects Before showing

Posted 15 July 2010 - 05:24 PM

How you handle this entirely depends on where your program takes time to load. Is it a visual lag, or is it lag in data access?

Figure this out first and then we'll tell you what you can do to solve your problem.
Was This Post Helpful? 0
  • +
  • -

#3 Rickster0  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 16
  • View blog
  • Posts: 236
  • Joined: 08-July 08

Re: Loading Form Objects Before showing

Posted 15 July 2010 - 05:33 PM

Its Visual Lag.

Data access it Absolutely fine at the moment, but the only type of lag i get is Visual, and its hardly even worked on, just a form with a few controls and a little MySQL connection, hardly anything i'd expect it to lag over.
Was This Post Helpful? 0
  • +
  • -

#4 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1526
  • View blog
  • Posts: 5,961
  • Joined: 21-March 08

Re: Loading Form Objects Before showing

Posted 15 July 2010 - 05:41 PM

can you post the code that you are doing that takes so long? Maybe we can help you optimize it.
Was This Post Helpful? 0
  • +
  • -

#5 Rickster0  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 16
  • View blog
  • Posts: 236
  • Joined: 08-July 08

Re: Loading Form Objects Before showing

Posted 15 July 2010 - 05:47 PM

Yeah. Sure thing.

This Is my Main Form, which is taking so long to load up.

using System;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;

namespace LegACy_Engine
{
    public partial class Main : Form
    {

        public Main()
        {
            InitializeComponent();
            Engine_Load();
        }

        void Engine_Load()
        {
            Thread th = new Thread(new ThreadStart(DoSplash),5);
            th.Start();
            Thread.Sleep(2000);
            th.Abort();
            Thread.Sleep(500);
        }

        static void DoSplash()
        {
            Splash sp = new Splash();
            sp.ShowDialog();
        }

        private void simpleButton1_Click(object sender, EventArgs e)
        {
            // Removes the Start Page
          //  xtraTabControl4.TabPages.Add("Project1");
            //xtraTabControl4.TabPages.RemoveAt(0);
        }

        // Login Button
        private void btnLogin_Click(object sender, EventArgs e)
        {
            try
            {
                // network connect settings. 
                Legacy_Network net = new Legacy_Network("localhost", "test", "root", "password");
                // begin connection.
                net.Connect();
                // If login Details are correct
                if (net.VerifyUser(textEdit1.Text, textEdit2.Text) == true)
                {
                    lblUsername.Text = textEdit1.Text;
                    team_logged.Text = "Logged in";
                    team_logged.ForeColor = Color.DarkGreen;
                    loginpanel.Visible = true;
                    alertControl1.AutoFormDelay = 3500;
                    alertControl1.Show(this, "Login Succesful", textEdit1.Text + " has logged into the Team Network");
                    MessageBox.Show("Login Successful, you are now logged in as: " + textEdit1.Text, "Logged In Successful");
                }
                // If a field is empty, show a error box.
                else if ((textEdit1.Text == string.Empty) || (textEdit2.Text == string.Empty))
                {
                    MessageBox.Show("A Field was left blank. please fill in both fields.", "Field Blank", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
                // Login unsuccesfull.
                else
                {
                    MessageBox.Show("Login Unsuccessful, The Username: " + textEdit1.Text + " does not exist", "Logged In Unscuccesful", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
                net.Disconnect(); 
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void btnRegister_Click(object sender, EventArgs e)
        {

        }

        private void Main_Load(object sender, EventArgs e)
        {
            timer1.Start();
            xtraTabControl4.Visible = true;
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            
            xtraTabControl1.Visible = true;
            xtraTabControl5.Visible = true;
            timer1.Dispose();
            //timer1.Enabled = false;
        }
    }
}



I used a C# profiler to see what was taking so long. and apparently this line

InitializeComponent();


Was the one taking up much more time than the rest of the project. i hope someone can help me fix this ^_^

This post has been edited by Rickster0: 15 July 2010 - 05:50 PM

Was This Post Helpful? 0
  • +
  • -

#6 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1526
  • View blog
  • Posts: 5,961
  • Joined: 21-March 08

Re: Loading Form Objects Before showing

Posted 15 July 2010 - 06:07 PM

InitializeComponent() adds all of the controls onto the form.

How many controls do you have? Can you post the code from the InitializeComponent method?

This post has been edited by eclipsed4utoo: 15 July 2010 - 06:07 PM

Was This Post Helpful? 0
  • +
  • -

#7 Rickster0  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 16
  • View blog
  • Posts: 236
  • Joined: 08-July 08

Re: Loading Form Objects Before showing

Posted 15 July 2010 - 06:16 PM

Ok, its Quite big. since i do have allot of controls :|

Spoiler


I can see why it takes so long to load now. Any way i can improve this?

This post has been edited by Rickster0: 15 July 2010 - 06:18 PM

Was This Post Helpful? 0
  • +
  • -

#8 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1526
  • View blog
  • Posts: 5,961
  • Joined: 21-March 08

Re: Loading Form Objects Before showing

Posted 15 July 2010 - 06:29 PM

Yeesh!! I am not sure. Is there a need for so many controls? Do you need everyone of them when the form loads? You could load some of them dynamically if all are not needed.
Was This Post Helpful? 0
  • +
  • -

#9 Rickster0  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 16
  • View blog
  • Posts: 236
  • Joined: 08-July 08

Re: Loading Form Objects Before showing

Posted 15 July 2010 - 06:34 PM

The controls that you currently see are for my Start Page. So i guess i could Need them at Start up. And every control is used.

Other than that, how would i go about loading them dynamically?

This post has been edited by Rickster0: 15 July 2010 - 06:35 PM

Was This Post Helpful? 0
  • +
  • -

#10 raziel_  Icon User is offline

  • Like a lollipop
  • member icon

Reputation: 465
  • View blog
  • Posts: 4,255
  • Joined: 25-March 09

Re: Loading Form Objects Before showing

Posted 16 July 2010 - 04:16 AM

ok is it me or this cause the time to load:
void Engine_Load()
        {
            Thread th = new Thread(new ThreadStart(DoSplash),5);
            th.Start();
            Thread.Sleep(2000);
            th.Abort();
            Thread.Sleep(500);
        }



you have 2 second sleep here
Was This Post Helpful? 0
  • +
  • -

#11 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1526
  • View blog
  • Posts: 5,961
  • Joined: 21-March 08

Re: Loading Form Objects Before showing

Posted 16 July 2010 - 04:41 AM

One way that you can test is to put a breakpoint on the InitializeComponent method call then start the application. When the breakpoint is hit, hit F10, and see how long it takes to complete. Then hit F10 again to see how long it takes the EngineLoad method to run. The EngineLoad method will take at least 2.5 seconds to complete.
Was This Post Helpful? 0
  • +
  • -

#12 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5677
  • View blog
  • Posts: 12,208
  • Joined: 02-June 10

Re: Loading Form Objects Before showing

Posted 16 July 2010 - 05:58 AM

View PostNoBrain, on 16 July 2010 - 03:16 AM, said:

ok is it me or this cause the time to load:
void Engine_Load()
        {
            Thread th = new Thread(new ThreadStart(DoSplash),5);
            th.Start();
            Thread.Sleep(2000);
            th.Abort();
            Thread.Sleep(500);
        }



you have 2 second sleep here


yeah... I was just looking at that myself. I can't guess what the OP is thinking here.
Rick: Why are you sleeping your thread for a total of 2.5 seconds here? This just makes your program look very unresponsive. You Do splash, then sleep. WTF?

Rick said:

i see that the splash screen is used to load objects {...}

SplashScreen don't load anything. They *report* the status of loading to keep the user calm so they don't think the program is locked up. Because you are sleeping your thread you are making it look locked up.

My suggestion: Your splash screen is a form and nothing more. It might have a fancy graphic or progress bar, but it is just a form that knows nothing about what your application is doing. It subscribes to an event in your loading class; something like SplashThis(string Message) and SplashPercent(decimal Remaining)

As your loading class is doing its job it occassionaly raises the SplashThis events like SplashPercent(43) and SplashThis("Loading maps...")


Quick and easy custom events
Bulding an application - Part 1
Was This Post Helpful? 0
  • +
  • -

#13 Rickster0  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 16
  • View blog
  • Posts: 236
  • Joined: 08-July 08

Re: Loading Form Objects Before showing

Posted 16 July 2010 - 06:19 AM

I did that because when i was reading a tutorial on how to create splash screens, this is how they did it. I guess i should change/remove that?
Was This Post Helpful? 0
  • +
  • -

#14 raziel_  Icon User is offline

  • Like a lollipop
  • member icon

Reputation: 465
  • View blog
  • Posts: 4,255
  • Joined: 25-March 09

Re: Loading Form Objects Before showing

Posted 16 July 2010 - 06:41 AM

the tutorial that you read probably show you how to hold your splash screen for a period of time. that is why they use the sleep. as tlhIn'toq say you can use them to notify the user that your program is doing something so they dont think it hang up.
Was This Post Helpful? 0
  • +
  • -

#15 Rickster0  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 16
  • View blog
  • Posts: 236
  • Joined: 08-July 08

Re: Loading Form Objects Before showing

Posted 16 July 2010 - 06:44 AM

Alright. So ill change that part then. Any other suggestions for speeding up that
InitializeComponent()
part?
Was This Post Helpful? 0
  • +
  • -

  • (3 Pages)
  • +
  • 1
  • 2
  • 3