不会消失的启动画面
我使用的是此处的启动屏幕。 我喜欢它是多么简单。 但它的问题是,除非我点击它,否则启动屏幕不会消失。 当在 IDE 中运行时,它工作正常。 有任何想法吗? 我将在此处附加代码,但由于某种原因它无法正确插入。
private System.Windows.Forms.Timer timer1;
//private Splash sp=null;
public Form1()
{
InitializeComponent();
Thread th = new Thread(new ThreadStart(DoSplash));
//th.ApartmentState = ApartmentState.STA;
//th.IsBackground=true;
th.Start();
Thread.Sleep(3000);
th.Abort();
Thread.Sleep(1000);
}
private void DoSplash()
{
Splash sp = new Splash();
sp.ShowDialog();
}
private void timer1_Tick(object sender, System.EventArgs e)
{
// sp.Close();
}
I'm using a Splash Screen from Here. I love how simple it is. But the problem with it is that the splash screen doesn't go away until I click on it. When run within the IDE it works fine. Any ideas? I'd attach the code here but its not inserting properly for some reason.
private System.Windows.Forms.Timer timer1;
//private Splash sp=null;
public Form1()
{
InitializeComponent();
Thread th = new Thread(new ThreadStart(DoSplash));
//th.ApartmentState = ApartmentState.STA;
//th.IsBackground=true;
th.Start();
Thread.Sleep(3000);
th.Abort();
Thread.Sleep(1000);
}
private void DoSplash()
{
Splash sp = new Splash();
sp.ShowDialog();
}
private void timer1_Tick(object sender, System.EventArgs e)
{
// sp.Close();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这是一个丑陋的实现。 您应该查看这个SO线程。 这描述了如何使用 C# 中的 VisualBasic.NET 命名空间和 OnCreateSplashScreen 方法可以实现更简洁的启动屏幕。
That's an ugly implementation. You should check out this SO thread. That describes how to use the VisualBasic.NET namespace from C# and the OnCreateSplashScreen method to do a much cleaner splash screen implementation.
首先,该页面上的启动屏幕的完成方式,使用 Thread.Abort,不是正确的处理方式。
切勿调用 Thread.Abort,除非您正在关闭 < a href="http://msdn.microsoft.com/en-us/library/system.appdomain.aspx" rel="nofollow noreferrer">AppDomain 线程所在的位置。
让我重申一下以强调这一点。 唯一一次应该调用 Thread.Abort 是当您足够了解 Thread.Abort 以及它的行为如何知道您永远不应该调用它。
看看 StackOverflow 上的另一个问题:c# 中的多线程启动屏幕?。
如果您想保留现有的解决方案,可能更好的方法是将计时器放入启动屏幕表单中,将其计时器设置为您希望启动屏幕保留在屏幕上的时间,然后调用 Close 在其 Tick 事件处理程序中。
在同一个地点,我会简单地关闭原来的线程,并删除其他线程。
换句话说,从该页面上的第一个代码块开始,我将保留这两行:
将其与表单上的计时器结合起来,使表单自动关闭,并且您的状态比尝试掌握要好得多与 Thread.Abort。
你不应该打电话。
First of all, the way the splash screen on that page is done, using Thread.Abort, is not the right way to do things.
Never call Thread.Abort, unless you're in the process of shutting down the AppDomain the thread lives in.
Let me reiterate that for emphasis. The only time you should call Thread.Abort is when you know enough about Thread.Abort and how it behaves to know that you should never call it.
Take a look at this other question on StackOverflow: Multi-Threaded splash screen in c#?.
If you want to keep your existing solution, a possible better way would be to drop a timer into the splash screen form, set its timer to the time you want the splash screen to stay on screen, and call Close in its Tick event handler.
In the same venue, I would simply fire off that original thread, and remove the other lines.
In other words, from the first code block on that page, I would keep these two lines:
Couple that with that timer on the form that makes the form self-closing, and you're in way better shape than trying to get to grips with Thread.Abort.
Which you should not call.
DoSplash 可能应该调用
sp.Show()
而不是sp.ShowDialog()
但是从主线程调用 Sleep(x000) 来显示启动屏幕并不是很好高效,并且 Thread.Abort() 也应该保留以便更好地使用。
更好的方法是使用计时器关闭 SplashForm 并设置最小延迟。 启动时,您可以 Show() 和 Update() SplahForm 并继续加载内容。 在 MainForm 完成初始化并进入 EventLoop 之前,计时器事件不会触发。 不需要线程,也不需要涉及 MainForm。
只是为了向您提供 MessageLoop 驱动的 SplashForm 的主要成分:
然后,在 Program.cs 中,
The DoSplash should probably call
sp.Show()
instead ofsp.ShowDialog()
But calling Sleep(x000) form your main thread to show a splash screen isn't very efficient, and Thread.Abort() should be reserved for better use too.
A better approach is to close your SplashForm with a Timer and set a minimum delay. On startup you can Show() and Update() the SplahForm and continue to load stuff. The timer event won't fire until the MainForm is finished initializing and enters the EventLoop. No threads needed, and the MainForm doesn't have to be involved either.
Just to give you the main ingredients of a MessageLoop driven SplashForm:
and then, in Program.cs,