pharaon450
pharaon450

Reputation: 523

How can i show an image while my application is loading

i have and application windows form .net and my form1 takes a lot of time to appear because in it's event form1_Load does a lot of operation.

My goal is to show an image while the operation are being done.

private void form1_Load(object sender, EventArgs e)
{            
    methode1();
}

While my methode1() is working, my form doesnt show, i want to show an image on the screen while my methode1() is working because while methode1() is working, there is nothing on the screen.

Upvotes: 2

Views: 10375

Answers (6)

Augustin Oros
Augustin Oros

Reputation: 11

Load an image in the project, go to properties and select Build Action : SplashScreen. Also select Copy to Output Directory as Copy always.

Upvotes: 1

Md Kamruzzaman Sarker
Md Kamruzzaman Sarker

Reputation: 2407

All the visual things in .net is done on form. You can do it by creating an small form which contains an image load it before module1() and after completing module1() close it. Just below..

private void form1_Load(object sender, EventArgs e)
{    
        Form f = new Form();
        f.Size = new Size(400, 10);
        f.FormBorderStyle = FormBorderStyle.None;
        f.MinimizeBox = false;
        f.MaximizeBox = false;
        Image im = Image.FromFile(path);
        PictureBox pb = new PictureBox();
        pb.Dock = DockStyle.Fill;
        pb.Image = im;
        pb.Location = new Point(5, 5);
        f.Controls.Add(pb);
        f.Show();        
        methode1();
        f.Close();
}

Upvotes: 2

Jupaol
Jupaol

Reputation: 21365

Try this code

using System.Reactive.Linq;

    private void RealForm_Load(object sender, EventArgs e)
    {
        var g = new Splash();

        // place in this delegate the call to your time consuming operation
        var timeConsumingOperation = Observable.Start(() => Thread.Sleep(5000));
        timeConsumingOperation.ObserveOn(this).Subscribe(x =>
        {
            g.Close();
            this.Visible = true;
        });

        this.Visible = false;
        g.ShowDialog();
    }

This code uses Microsoft Rx to execute operations in background threads among other cool features

http://msdn.microsoft.com/en-us/data/gg577609.aspx

In order for this code to work you need to reference two nuget packages: Rx and Rx windows forms

https://nuget.org/packages/Rx-Main/1.0.11226

https://nuget.org/packages/Rx-WinForms/1.0.11226

Upvotes: 2

embedded.kyle
embedded.kyle

Reputation: 11466

How about using the built in SplashScreen class?

http://msdn.microsoft.com/en-us/library/system.windows.splashscreen.aspx

Upvotes: 0

Denis
Denis

Reputation: 12077

(splash screen c# -- google it)

Here's what I just found: http://msdn.microsoft.com/en-us/library/aa446493.aspx

Upvotes: 0

Matzi
Matzi

Reputation: 13925

Create another form, just for loading, with a static image, and display it before your application starts to load, and destroy it afterwards. Always on top, and with no border is the usual setup for such things.

Upvotes: 2

Related Questions