Why is my form so shy?
EDIT 2
Ok, based on the guidelines below, I have eliminated my threading approach and my program now looks like this: 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);
}
And FrmSplash.cs like this:
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;
}
The problem is, it doesn't work right now. The popup screen pops up for a second of a second, the progress bar of the brand never initializes and then disappears while I wait 10 seconds for the dll and Main Form to show up without looking at anything ....
The color of me is very confusing now!
ORIGINAL MAIL → for reference
I've implemented a popup application loading screen that runs on a separate thread until all DLLs are loaded and the form gets Painted. This works as expected. The weird thing is that now when the Splash form exits, it submits my main form back if there is anything else open (like Outlook). I am starting a thread in 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();
}
}
And then I end it when the FrmSearch_Shown event fires.
private void FrmSearch_Shown(object sender, EventArgs e)
{
Program.splashThread.Abort();
this.Show();
this.BringToFront();
}
I, as you can see, tried to call Show () and / or BringToFront () in FrmSearch and still "jumps" back.
What am I missing?
What else can I try?
I am doing this so terribly ignorant that it is my process causing this?
Do I have to apply for early retirement?
Thank you for understanding!
EDIT 1
I tried setting the TopMost property on my main form to TRUE. This hides my form from hiding, but it also allows the user to look at any other application. It seems a little narcissistic to me ...
a source to share
First of all, it is very important that the UI work is done on the main thread of the application. I'm really surprised that you don't get more severe errors anymore when showing the splash screen on a background thread.
Here is the technique I used:
Use Application.Run on your splash form, not your "real" form.
In your splash form, enter an initialized event:
public event EventHandler SplashFormInitialized
Create a timer that fires in one millisecond and fires this event.
Then in your application run method you can load your real form, then close your splash form and execute Application.Run on real form
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);
A splash can include:
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;
}
a source to share
Try calling Application.DoEvents () right after the show.
Warning: Don't call DoEvents very often, but this is one of those cases.
EDIT: Clyde noticed something that I didn't: you thread through it. Don't run any UI on a different thread. Take out the thread, leave in Application.DoEvents ().
a source to share