C# 使用计时器创建飞溅屏幕/加载屏幕
我创建了一个工作的C# 使用计时器创建飞溅屏幕/加载屏幕,c#,forms,timer,splash-screen,C#,Forms,Timer,Splash Screen,我创建了一个工作的飞溅屏幕/加载屏幕 我使用以下代码显示和关闭LoadinScreen: LoadingScreen LS = new LoadingScreen(); LS.Show(); databaseThread = new Thread(CheckDataBase); databaseThread.Start(); databaseThread.Join(); LS.Close(); 这段代码对我来说做得很好,显示并关闭加载屏幕 问题是:我在加
飞溅屏幕/加载屏幕
我使用以下代码显示和关闭LoadinScreen:
LoadingScreen LS = new LoadingScreen();
LS.Show();
databaseThread = new Thread(CheckDataBase);
databaseThread.Start();
databaseThread.Join();
LS.Close();
这段代码对我来说做得很好,显示并关闭加载屏幕
问题是:我在加载屏幕上看到一些文本,上面写着:加载应用程序…
我想创建一个计时器,让文本(标签)末尾的点执行以下操作:
Loading Application.
1秒后:
Loading Application..
1秒后:
Loading Application...
我想我需要在加载屏幕窗体的加载事件中添加一个计时器
我怎样才能做到这一点呢?应该简单到:
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;
}
});
}
}
你有没有考虑过一个简单的动画GIF?嗯,这是一个聪明的解决方案,它会搜索一些加载。GIF’有一件事,GIF会挂起,因为它正在线程中运行。你确定吗?一切都在线程中运行。不管怎样,我用BackGroundWorkerMmh解决了这个问题?每秒将触发已用事件(请注意构造函数中的“1000”值)。每次触发时,文本框内容都会更新,如:Loading。加载。。正在加载…嗯。。你部分是对的,因为我从来没有用“加载”来初始化文本框内容。只需将mytextbox.Text=“加载”;在timer0.appeased+=***之前,我最后有一个问题,我认为这永远不会起作用,因为我正在使用一个线程,而整个表单在该线程工作时挂起。这是因为您正在将主(UI)线程与数据库线程连接。您可以创建另一个等待databaseThread结束的线程,或者(更好的方式,imho)让databaseThread在完成后关闭加载splashscreen并让UI线程运行。。更好的是,您可以将其放入BackgroundWorker而不是线程中。然后,您可以在RunWorkerCompleted事件中关闭启动屏幕。您有一个与后台工作程序结合的启动屏幕的好例子吗?因为我找不到。
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();
}
}