使用计时器创建启动画面/加载屏幕

时间:2013-03-05 13:50:26

标签: c# forms timer splash-screen

我创建了一个有效的SplashScreen/LoadingScreen

我使用以下代码显示和关闭LoadinScreen:

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

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

   LS.Close();

此代码对我来说非常棒,显示并关闭LoadingScreen

问题是:我在LoadingScreen上收到了一些文字,上面写着:Loading Application...

我想创建一个Timer,让文本末尾的点(Label)执行以下操作:

Loading Application.

1秒后:

Loading Application..

1秒后来:

Loading Application...

我想我需要在timer的{​​{1}}添加Load_event

我怎样才能做到这一点?

2 个答案:

答案 0 :(得分:0)

应该如此简单:

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

答案 1 :(得分:0)

也许是这样的?

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

修改 防止UI线程阻塞等待数据库操作的好方法是将数据库操作移动到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();
    }
}