简体   繁体   中英

Creating a SplashScreen/LoadingScreen with timer

I created a working SplashScreen/LoadingScreen .

I used the following code to show and close the LoadinScreen:

   LoadingScreen LS = new LoadingScreen();
   LS.Show();

   databaseThread = new Thread(CheckDataBase);
   databaseThread.Start();
   databaseThread.Join();

   LS.Close();

This code is doing a great job for me, showing and closing the LoadingScreen .

The problem is: I got some text on the LoadingScreen , that says: Loading Application...

I want to create a Timer to let the dots at the end of the text(Label) do the following:

Loading Application.

1 second later:

Loading Application..

1 second Later:

Loading Application...

I suppose that I need to add a timer to the Load_event of the LoadingScreen form .

How can I achieve this?

It should be as simple as:

Timer timer = new Timer();
timer.Interval = 300;
timer.Tick += new EventHandler(methodToUpdateText);
timer.Start();

Maybe something like this?

class LoadingScreen
{
    Timer timer0;
    TextBox mytextbox = new TextBox();
    public LoadingScreen()
    {
        timer0 = new System.Timers.Timer(1000);
        timer0.Enabled = true;
        timer0.Elapsed += new Action<object, System.Timers.ElapsedEventArgs>((object sender, System.Timers.ElapsedEventArgs e) =>
        {
            switch (mytextbox.Text) 
            {
                case "Loading":
                    mytextbox.Text = "Loading.";
                    break;
                case "Loading.":
                    mytextbox.Text = "Loading..";
                    break;
                case "Loading..":
                    mytextbox.Text = "Loading...";
                    break;
                case "Loading...":
                    mytextbox.Text = "Loading";
                    break;
            }
        });
    }
}

Edit: A good way to prevent UI thread to block waiting for database operation is to move the database operation to a BackgroundWorker ex:

public partial class App : Application
{
    LoadingScreen LS;   
    public void Main()
    {
        System.ComponentModel.BackgroundWorker BW;
        BW.DoWork += BW_DoWork;
        BW.RunWorkerCompleted += BW_RunWorkerCompleted;
        LS = new LoadingScreen();
        LS.Show();
    }

    private void BW_DoWork(System.Object sender, System.ComponentModel.DoWorkEventArgs e)
    {
        //Do here anything you have to do with the database
    }

    void BW_RunWorkerCompleted(object sender, System.ComponentModel.RunWorkerCompletedEventArgs e)
    {
        LS.Close();
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM