469,964 Members | 1,754 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,964 developers. It's quick & easy.

how to start and show a window?

Hi,
I have windows form program. While that form is building to show, I
want to show another window (kinda splash screen so I can show the user
the program is loading..)
Can someone suggest a good approach?
I did the following at present and is not working.

In the main form (CAB project) right before base.run(), I started a
seperate thread and launched a windows form and it did show up (in the
task bar not the window though) and closes immediatly.

Is there something I need to know how to show a window in a seperate
thread?

Thanks.

Aug 27 '06 #1
4 1796
What you describe should work; are you perhaps using .Show() to display
the form? If so, the second thread will reach the end of it's
ThreadStart method, and will terminate, tearing down the form. The way
to handle this is, for instance, to call ShowDialog() on the splash
form (so that it remains open), and then (when ready) ask the splash
form to close itself (ending that thread).

Here's a handy wrapper for all of this using generics and
IDisposable... just replace <MySplashFormwith <YourForm>...

class MySplashForm : Form {
public MySplashForm() {
Text = "I'm a splash form";
FormBorderStyle = FormBorderStyle.FixedDialog;
ShowInTaskbar = MinimizeBox = MaximizeBox = false;
}
}
static void Main() {
using (Splash<MySplashFormloader = new
Splash<MySplashForm>()) {
Thread.Sleep(10000);
}
MessageBox.Show("That do?");
}
class Splash<T: IDisposable where T : Form, new() {
T _form;
public Splash() {
Thread thread = new Thread(Show);
thread.IsBackground = true;
thread.Start();
}
private void Show() {
_form = new T();
using (T form = _form) {
form.ShowDialog();
} // to ensure disposed on exit
}
private void Close() {
try { _form.Close(); } catch { } // swallow
}
public void Dispose() {
if (_form != null) {
try { _form.Invoke((MethodInvoker) this.Close); } catch
{ } // swallow
_form = null;
}
}
}

Aug 27 '06 #2
Forgot to say; the Thread.Sleep(10000) represents your code loading;
you might choose to dispose of the loader in the Load event of your
form, although it can be simpler if you simply wrap an "Initialize"
form method with the splash code.

also - you can use the alternative "using" syntax (without a variable):

using (new Splash<MySplashForm>()) {
Thread.Sleep(10000);
}

Marc

Aug 27 '06 #3
Marc,
Excellent wrapper.
Thanks a lot.

Marc Gravell wrote:
Forgot to say; the Thread.Sleep(10000) represents your code loading;
you might choose to dispose of the loader in the Load event of your
form, although it can be simpler if you simply wrap an "Initialize"
form method with the splash code.

also - you can use the alternative "using" syntax (without a variable):

using (new Splash<MySplashForm>()) {
Thread.Sleep(10000);
}

Marc
Aug 27 '06 #4
You are most welcome.

Marc

Aug 27 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Richard Lionheart | last post: by
19 posts views Thread by Oliver Neumann | last post: by
6 posts views Thread by Dde | last post: by
10 posts views Thread by Stephany Young | last post: by
2 posts views Thread by =?Utf-8?B?Vmlua2k=?= | last post: by
14 posts views Thread by shark | last post: by
3 posts views Thread by Bali | last post: by
1 post views Thread by rainxy | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.