How can i show an image while my application is loading

风流意气都作罢 提交于 2019-12-22 18:26:35

问题


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.


回答1:


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();
}



回答2:


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.




回答3:


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




回答4:


(splash screen c# -- google it)

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




回答5:


How about using the built in SplashScreen class?

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



来源:https://stackoverflow.com/questions/10823107/how-can-i-show-an-image-while-my-application-is-loading

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!