0

我创建了一个工作SplashScreen/LoadingScreen.

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

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

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

   LS.Close();

这段代码对我来说做得很好,显示和关闭LoadingScreen.

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

我想创建一个计时器,让文本(标签)末尾的点执行以下操作:

Loading Application.

1 秒后:

Loading Application..

1 秒后:

Loading Application...

我想我timer需要Load_eventLoadingScreen form.

我怎样才能做到这一点?

4

2 回答 2

0

它应该很简单:

Timer timer = new Timer();
timer.Interval = 300;
timer.Tick += new EventHandler(methodToUpdateText);
timer.Start();
于 2013-03-05T14:00:18.317 回答
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 例如:

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();
    }
}
于 2013-03-05T14:03:56.563 回答