2013-03-05 27 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();