Application.Run() without parameters, form doesn't stay open.

  • Thread starter Thread starter Oleg Ogurok
  • Start date Start date
O

Oleg Ogurok

Hi there,

I need to start my Windows Forms application without showing the form
initially. Then at some point, the application will be signaled to
generate and display the user interface.
I'm trying to use Application.Run() without parameters. Since this
method doesn't return I start a separate thread. The thread sleeps for
2 secs to simulate the delay and then tries to open the main form.

Somehow, with my code, the form show up for a split second and then
disappears.

Your help is greatly appreciated. I'm using .NET 1.1.

public class Program
{
public Program()
{
}

static Thread t;
static Form1 f;

/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
t = new Thread(new ThreadStart(StartUI));
t.Start();
Application.Run();
}

private static void StartUI()
{
Thread.Sleep(2000); // simulate delay
f = new Form1();
f.HandleCreated += new EventHandler(f_HandleCreated);
IntPtr p = f.Handle;
}

public delegate void ShowDelegate();
private static void f_HandleCreated(object sender, EventArgs e)
{
f.Invoke( new ShowDelegate(f.Show));
}
}
 
You don;t need Application.Run in this case - just do what you need and call
..Run method when you need to show the form using form reference. And remove
this thread stuff - you don't need it either.

Look at the STAThread description and note that forms are designed to run on
UI thread. If you are trying to make another thread perform as UI - I am not
sure it will work as expected.

You can start another thread doing preparation work and wait until it is
complete or use some event to signal back it is finished. But run the form
on main thread to avoid surprises.

What is the real reason to deviate from recommended way?
 
I'm just trying to start the application silently without showing the
main form right away. Then at some point the UI will be shown.


You don;t need Application.Run in this case - just do what you need and call
.Run method when you need to show the form using form reference. And remove
this thread stuff - you don't need it either.

Look at the STAThread description and note that forms are designed to run on
UI thread. If you are trying to make another thread perform as UI - I am not
sure it will work as expected.

You can start another thread doing preparation work and wait until it is
complete or use some event to signal back it is finished. But run the form
on main thread to avoid surprises.

What is the real reason to deviate from recommended way?




Hi there,
I need to start my Windows Forms application without showing the form
initially. Then at some point, the application will be signaled to
generate and display the user interface.
I'm trying to use Application.Run() without parameters. Since this
method doesn't return I start a separate thread. The thread sleeps for
2 secs to simulate the delay and then tries to open the main form.
Somehow, with my code, the form show up for a split second and then
disappears.
Your help is greatly appreciated. I'm using .NET 1.1.
public class Program
{
public Program()
{
}
static Thread t;
static Form1 f;
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
t = new Thread(new ThreadStart(StartUI));
t.Start();
Application.Run();
}
private static void StartUI()
{
Thread.Sleep(2000); // simulate delay
f = new Form1();
f.HandleCreated += new EventHandler(f_HandleCreated);
IntPtr p = f.Handle;
}
public delegate void ShowDelegate();
private static void f_HandleCreated(object sender, EventArgs e)
{
f.Invoke( new ShowDelegate(f.Show));
}
}- Hide quoted text -

- Show quoted text -
 
Back
Top