编辑2
好的,根据以下答案的建议,我取消了线程方法,现在我的程序如下所示:
program.cs
static void Main(){
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
FrmWWCShell FrmWWCShell = null;
var splash = new FrmSplash();
splash.SplashFormInitialized += delegate
{
FrmWWCShell = new FrmWWCShell();
splash.Close();
};
Application.Run(splash);
Application.Run(FrmWWCShell);
}
和FrmSplash.cs像这样:
public partial class FrmSplash : Form
{
public FrmSplash()
{
InitializeComponent();
}
protected override void OnLoad(EventArgs e)
{
splashTimer.Interval = 1;
splashTimer.Tick +=
delegate { if (SplashFormInitialized != null) SplashFormInitialized(this, EventArgs.Empty); };
splashTimer.Enabled = true;
}
public event EventHandler SplashFormInitialized;
}
问题在于它现在根本不起作用。初始屏幕突然跳出,字幕进度条甚至从未初始化,然后消失,当我等待10秒让dll和Main Form出现而又什么都没看见的时候。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。
为我着色严重混淆了!
原始帖子->供引用
我实现了一个“应用程序加载”启动屏幕,该屏幕在所有dll都加载且表单被“绘制”时在单独的线程上运行。那按预期工作。奇怪的是,现在在启动表单退出时,如果还有其他打开的地方(即Outlook),它将我的主表单发送到后面。我在Program.cs中启动线程,
static class Program
{
public static Thread splashThread;
[STAThread]
static void Main()
{
splashThread = new Thread(doSplash);
splashThread.Start();
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new FrmWWCShell());
}
private static void doSplash()
{
var splashForm = new FrmSplash();
splashForm.ShowDialog();
}
}
然后,一旦我的FrmSearch_Shown事件被触发,就结束它。
private void FrmSearch_Shown(object sender, EventArgs e)
{
Program.splashThread.Abort();
this.Show();
this.BringToFront();
}
如您所见,我尝试在FrmSearch上调用Show()和/或BringToFront(),但它仍然“跳转”到后面。
我想念什么?
我还能尝试什么?
我这样做太无知了,是我的过程导致了这一点吗?
我应该申请提早退休吗?
感谢您的任何见解!
编辑1
我尝试将主窗体上的TopMost属性设置为TRUE。这可以防止隐藏我的表单,但也可以防止用户查看其他任何应用程序。似乎对我有点自恋...
最佳答案
首先,在主应用程序线程上完成UI工作非常重要。通过在后台线程上显示初始屏幕,您还没有收到更严重的错误,实际上让我感到惊讶。
这是我使用的一种技术:
使用Application.Run在您的初始窗体而不是您的“实际”窗体上。
在您的启动表单中,有一个初始化事件:
public event EventHandler SplashFormInitialized
创建一个在1毫秒内触发并触发该事件的计时器。
然后在您的应用程序运行方法中,您可以加载您的真实表单,然后关闭您的启动表单并执行一个应用程序。
var realForm = null;
var splash = new SplashForm();
splash.SplashFormInitialized += delegate {
// As long as you use a system.windows.forms.Timer in the splash form, this
// handler will be called on the UI thread
realForm = new FrmWWCShell();
//do any other init
splash.Close();
}
Application.Run(splash); //will block until the splash form is closed
Application.Run(realForm);
飞溅可能包括:
overrides OnLoad(...)
{
/* Using a timer will let the splash screen load and display itself before
calling this handler
*/
timer.Interval = 1;
timer.Tick += delegate {
if (SplashFormInitialized != null) SplashFormInitialized(this, EventArgs.Empty);
};
timer.Enabled = true;
}
关于c# - 为什么我的表格这么害羞?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/869729/