[英]Creating a SplashScreen/LoadingScreen with timer
我創建了一個工作的SplashScreen/LoadingScreen
。
我使用以下代碼顯示和關閉LoadinScreen:
LoadingScreen LS = new LoadingScreen();
LS.Show();
databaseThread = new Thread(CheckDataBase);
databaseThread.Start();
databaseThread.Join();
LS.Close();
這段代碼對我來說非常LoadingScreen
,它顯示並關閉了LoadingScreen
。
問題是:我在LoadingScreen
上看到一些文本,說: Loading Application...
我想創建一個計時器,以使text(Label)末尾的點執行以下操作:
Loading Application.
1秒后:
Loading Application..
1秒后:
Loading Application...
我想我需要在LoadingScreen form
的Load_event
中添加一個timer
。
我該如何實現?
它應該很簡單:
Timer timer = new Timer();
timer.Interval = 300;
timer.Tick += new EventHandler(methodToUpdateText);
timer.Start();
也許是這樣的嗎?
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();
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.