2013-03-05 69 views
0

我创建了一个工作SplashScreen/LoadingScreen用定时器创建一个SplashScreen/LoadingScreenscreen

我用下面的代码来显示和关闭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... 

我想我需要添加一个timerLoadingScreen formLoad_event

我怎样才能做到这一点?

+1

你有没有考虑一个简单的动画GIF? – 2013-03-05 14:08:28

+0

嗯,这是一个聪明的解决方案,将搜索一些加载.GIF的 – Max 2013-03-05 14:10:33

+0

有一件事,GIF将挂起,因为它在一个线程中运行。 – Max 2013-03-05 14:56:35

回答

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

这种方法不行,因为它会自动只显示Loading ... – Max 2013-03-05 14:13:09

+0

Mmh?已消逝的事件将每秒触发(注意构造函数中的“1000”值)。每次触发时,文本框内容将会更新,如: 正在加载 正在加载。 正在加载.. 正在加载... – Jamby 2013-03-05 14:20:11

+0

嗯..你部分正确,因为我从来没有用“加载”初始化文本框内容。简单地说mytextbox.Text =“加载”;之前timer0.Elapsed + = *** – Jamby 2013-03-05 14:22:52

0

它应该是简单的:

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