473,383 Members | 1,737 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

Windows Forms and Peek-a-boo!

Excetremely annoying problem, I've an application with a long startup time.
So I created another form with my logo in it to as a splash screen.
The splash screen is run from another thread and is communicated solely
through static method and Invoke()'s
However, when I close my second form, the first one (main window) is hiding
under all the windows on the desktop.
If I don't close the splash screen, then everything is fine.

I tried everything, but it's just not working.

//Form1.cs - the main window of my app

using System;

using System.Drawing;

using System.Collections;

using System.ComponentModel;

using System.Windows.Forms;

using System.Data;

using System.Drawing.Printing;

namespace PrintTest

{

/// <summary>

/// Summary description for Form1.

/// </summary>

public class Form1 : System.Windows.Forms.Form

{

/// <summary>

/// Required designer variable.

/// </summary>

private System.ComponentModel.Container components = null;

private System.Windows.Forms.Button button1;

public Form1()

{

//

// Required for Windows Form Designer support

//

InitializeComponent();
}

/// <summary>

/// Clean up any resources being used.

/// </summary>

protected override void Dispose( bool disposing )

{

if( disposing )

{

if (components != null)

{

components.Dispose();

}

}

base.Dispose( disposing );

}

#region Windows Form Designer generated code

/// <summary>

/// Required method for Designer support - do not modify

/// the contents of this method with the code editor.

/// </summary>

private void InitializeComponent()

{

this.button1 = new System.Windows.Forms.Button();

this.SuspendLayout();

//

// button1

//

this.button1.Location = new System.Drawing.Point(96, 56);

this.button1.Name = "button1";

this.button1.Size = new System.Drawing.Size(152, 104);

this.button1.TabIndex = 0;

this.button1.Text = "button1";

this.button1.Click += new System.EventHandler(this.button1_Click);

//

// Form1

//

this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);

this.ClientSize = new System.Drawing.Size(292, 266);

this.Controls.AddRange(new System.Windows.Forms.Control[] {

this.button1});

this.Name = "Form1";

this.Text = "Form1";

this.ResumeLayout(false);

}

#endregion

/// <summary>

/// The main entry point for the application.

/// </summary>

[STAThread]

static void Main()

{

Form2.Dispaly();

System.Threading.Thread.Sleep(5000);

Form2.Vanish();

Application.Run(new Form1());

}

private void button1_Click(object sender, System.EventArgs e)

{
}
}

}

// Form2.cs - the splash screen

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Threading;

namespace PrintTest
{
/// <summary>
/// Summary description for Form2.
/// </summary>
public class Form2 : System.Windows.Forms.Form
{
private System.ComponentModel.IContainer components;
private static Form2 That;
private static bool Open;
private System.Windows.Forms.Timer Timer1;
public Form2()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
Timer1.Enabled=true;

}

/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.Timer1 = new System.Windows.Forms.Timer(this.components);
//
// Timer1
//
this.Timer1.Tick += new System.EventHandler(this.Timer1_Tick);
//
// Form2
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(292, 266);
this.Name = "Form2";
this.Opacity = 0;
this.Text = "Form2";
this.TopMost = true;

}
#endregion

public static void Start()
{
That=new Form2();
//That.Show();//doesn't work
//That.ShowDialog();//works, but hide the main window when exiting
Application.Run(That);//works, but hide the main window when exiting
}

public static void Dispaly()
{
if(Open)
return;
Open=true;
Thread t = new Thread(new ThreadStart(new MethodInvoker(Start)));
t.Start();
}
public static void Vanish()
{
That.Invoke(new MethodInvoker(VanishInvoked));
}
private static void VanishInvoked()
{
Open = false;
That.Timer1.Enabled = true;
}

private void Timer1_Tick(object sender, System.EventArgs e)
{
if(Open)
{
this.Opacity+=0.1;
if(That.Opacity>=100)
Timer1.Enabled=false;
}
else
{
if(this.Opacity>25)
this.Opacity-=0.1;
else
{
this.Close();
That = null;//free resources
}
}
}
}
}

Nov 15 '05 #1
6 2500
Hi,

You are messing with visual controls in non UI thread (method Start) which
is usually a very bad thing to do.
IOW that's your main problem.
Plus, I hope that you are aware that the line
System.Threading.Thread.Sleep(5000);
stops your thread for 5 seconds thus your app start time is 5 second longer.

--
Miha Markic - RightHand .NET consulting & software development
miha at rthand com

"Ayende Rahien" <Ay****@no.spam> wrote in message
news:eQ*************@TK2MSFTNGP09.phx.gbl...
Excetremely annoying problem, I've an application with a long startup time. So I created another form with my logo in it to as a splash screen.
The splash screen is run from another thread and is communicated solely
through static method and Invoke()'s
However, when I close my second form, the first one (main window) is hiding under all the windows on the desktop.
If I don't close the splash screen, then everything is fine.

Nov 15 '05 #2
Ayende,

Have you tried to call Dispose on the Form2 instance before you are done
with it?

Also, I think that you should have two application loops (i.e. call the
static Run method on the Application class). One to handle your splash
screen (which would then start the thread), and then when that is done, call
Run again, with your new form.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Ayende Rahien" <Ay****@no.spam> wrote in message
news:eQ*************@TK2MSFTNGP09.phx.gbl...
Excetremely annoying problem, I've an application with a long startup time. So I created another form with my logo in it to as a splash screen.
The splash screen is run from another thread and is communicated solely
through static method and Invoke()'s
However, when I close my second form, the first one (main window) is hiding under all the windows on the desktop.
If I don't close the splash screen, then everything is fine.

I tried everything, but it's just not working.

//Form1.cs - the main window of my app

using System;

using System.Drawing;

using System.Collections;

using System.ComponentModel;

using System.Windows.Forms;

using System.Data;

using System.Drawing.Printing;

namespace PrintTest

{

/// <summary>

/// Summary description for Form1.

/// </summary>

public class Form1 : System.Windows.Forms.Form

{

/// <summary>

/// Required designer variable.

/// </summary>

private System.ComponentModel.Container components = null;

private System.Windows.Forms.Button button1;

public Form1()

{

//

// Required for Windows Form Designer support

//

InitializeComponent();
}

/// <summary>

/// Clean up any resources being used.

/// </summary>

protected override void Dispose( bool disposing )

{

if( disposing )

{

if (components != null)

{

components.Dispose();

}

}

base.Dispose( disposing );

}

#region Windows Form Designer generated code

/// <summary>

/// Required method for Designer support - do not modify

/// the contents of this method with the code editor.

/// </summary>

private void InitializeComponent()

{

this.button1 = new System.Windows.Forms.Button();

this.SuspendLayout();

//

// button1

//

this.button1.Location = new System.Drawing.Point(96, 56);

this.button1.Name = "button1";

this.button1.Size = new System.Drawing.Size(152, 104);

this.button1.TabIndex = 0;

this.button1.Text = "button1";

this.button1.Click += new System.EventHandler(this.button1_Click);

//

// Form1

//

this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);

this.ClientSize = new System.Drawing.Size(292, 266);

this.Controls.AddRange(new System.Windows.Forms.Control[] {

this.button1});

this.Name = "Form1";

this.Text = "Form1";

this.ResumeLayout(false);

}

#endregion

/// <summary>

/// The main entry point for the application.

/// </summary>

[STAThread]

static void Main()

{

Form2.Dispaly();

System.Threading.Thread.Sleep(5000);

Form2.Vanish();

Application.Run(new Form1());

}

private void button1_Click(object sender, System.EventArgs e)

{
}
}

}

// Form2.cs - the splash screen

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Threading;

namespace PrintTest
{
/// <summary>
/// Summary description for Form2.
/// </summary>
public class Form2 : System.Windows.Forms.Form
{
private System.ComponentModel.IContainer components;
private static Form2 That;
private static bool Open;
private System.Windows.Forms.Timer Timer1;
public Form2()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
Timer1.Enabled=true;

}

/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.Timer1 = new System.Windows.Forms.Timer(this.components);
//
// Timer1
//
this.Timer1.Tick += new System.EventHandler(this.Timer1_Tick);
//
// Form2
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(292, 266);
this.Name = "Form2";
this.Opacity = 0;
this.Text = "Form2";
this.TopMost = true;

}
#endregion

public static void Start()
{
That=new Form2();
//That.Show();//doesn't work
//That.ShowDialog();//works, but hide the main window when exiting
Application.Run(That);//works, but hide the main window when exiting
}

public static void Dispaly()
{
if(Open)
return;
Open=true;
Thread t = new Thread(new ThreadStart(new MethodInvoker(Start)));
t.Start();
}
public static void Vanish()
{
That.Invoke(new MethodInvoker(VanishInvoked));
}
private static void VanishInvoked()
{
Open = false;
That.Timer1.Enabled = true;
}

private void Timer1_Tick(object sender, System.EventArgs e)
{
if(Open)
{
this.Opacity+=0.1;
if(That.Opacity>=100)
Timer1.Enabled=false;
}
else
{
if(this.Opacity>25)
this.Opacity-=0.1;
else
{
this.Close();
That = null;//free resources
}
}
}
}
}

Nov 15 '05 #3
I had this same problem, but I was able to find a pretty decent workaround
using the API. I use the following:

The splash form is handed the window handle of the main form just as the
main form completes its startup work (a public property on the splash form
accepts this).

Then, the splash form uses:
GetWindowThreadProcessId
followed by "AttachThreadInput" to hook the window message handlers together
Then is calls "SetActiveWindow", and then uses AttachThreadInput to put the
window message handlers back the way they were.

This has been working fine, and ends up with the main form active.

Jerry
"Ayende Rahien" <Ay****@no.spam> wrote in message
news:eQ*************@TK2MSFTNGP09.phx.gbl...
Excetremely annoying problem, I've an application with a long startup time. So I created another form with my logo in it to as a splash screen.
The splash screen is run from another thread and is communicated solely
through static method and Invoke()'s
However, when I close my second form, the first one (main window) is hiding under all the windows on the desktop.
If I don't close the splash screen, then everything is fine.

I tried everything, but it's just not working.

//Form1.cs - the main window of my app

using System;

using System.Drawing;

using System.Collections;

using System.ComponentModel;

using System.Windows.Forms;

using System.Data;

using System.Drawing.Printing;

namespace PrintTest

{

/// <summary>

/// Summary description for Form1.

/// </summary>

public class Form1 : System.Windows.Forms.Form

{

/// <summary>

/// Required designer variable.

/// </summary>

private System.ComponentModel.Container components = null;

private System.Windows.Forms.Button button1;

public Form1()

{

//

// Required for Windows Form Designer support

//

InitializeComponent();
}

/// <summary>

/// Clean up any resources being used.

/// </summary>

protected override void Dispose( bool disposing )

{

if( disposing )

{

if (components != null)

{

components.Dispose();

}

}

base.Dispose( disposing );

}

#region Windows Form Designer generated code

/// <summary>

/// Required method for Designer support - do not modify

/// the contents of this method with the code editor.

/// </summary>

private void InitializeComponent()

{

this.button1 = new System.Windows.Forms.Button();

this.SuspendLayout();

//

// button1

//

this.button1.Location = new System.Drawing.Point(96, 56);

this.button1.Name = "button1";

this.button1.Size = new System.Drawing.Size(152, 104);

this.button1.TabIndex = 0;

this.button1.Text = "button1";

this.button1.Click += new System.EventHandler(this.button1_Click);

//

// Form1

//

this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);

this.ClientSize = new System.Drawing.Size(292, 266);

this.Controls.AddRange(new System.Windows.Forms.Control[] {

this.button1});

this.Name = "Form1";

this.Text = "Form1";

this.ResumeLayout(false);

}

#endregion

/// <summary>

/// The main entry point for the application.

/// </summary>

[STAThread]

static void Main()

{

Form2.Dispaly();

System.Threading.Thread.Sleep(5000);

Form2.Vanish();

Application.Run(new Form1());

}

private void button1_Click(object sender, System.EventArgs e)

{
}
}

}

// Form2.cs - the splash screen

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Threading;

namespace PrintTest
{
/// <summary>
/// Summary description for Form2.
/// </summary>
public class Form2 : System.Windows.Forms.Form
{
private System.ComponentModel.IContainer components;
private static Form2 That;
private static bool Open;
private System.Windows.Forms.Timer Timer1;
public Form2()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
Timer1.Enabled=true;

}

/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.Timer1 = new System.Windows.Forms.Timer(this.components);
//
// Timer1
//
this.Timer1.Tick += new System.EventHandler(this.Timer1_Tick);
//
// Form2
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(292, 266);
this.Name = "Form2";
this.Opacity = 0;
this.Text = "Form2";
this.TopMost = true;

}
#endregion

public static void Start()
{
That=new Form2();
//That.Show();//doesn't work
//That.ShowDialog();//works, but hide the main window when exiting
Application.Run(That);//works, but hide the main window when exiting
}

public static void Dispaly()
{
if(Open)
return;
Open=true;
Thread t = new Thread(new ThreadStart(new MethodInvoker(Start)));
t.Start();
}
public static void Vanish()
{
That.Invoke(new MethodInvoker(VanishInvoked));
}
private static void VanishInvoked()
{
Open = false;
That.Timer1.Enabled = true;
}

private void Timer1_Tick(object sender, System.EventArgs e)
{
if(Open)
{
this.Opacity+=0.1;
if(That.Opacity>=100)
Timer1.Enabled=false;
}
else
{
if(this.Opacity>25)
this.Opacity-=0.1;
else
{
this.Close();
That = null;//free resources
}
}
}
}
}

Nov 15 '05 #4
"Miha Markic" <miha at rthand com> wrote in message
news:e7**************@TK2MSFTNGP11.phx.gbl...
Hi,

You are messing with visual controls in non UI thread (method Start) which
is usually a very bad thing to do.
IOW that's your main problem.
I fixed it so everything is Invoke()ed, but it doesn't help.
Plus, I hope that you are aware that the line
System.Threading.Thread.Sleep(5000);
stops your thread for 5 seconds thus your app start time is 5 second longer.

Yes, that is a test app to show the problem, I want a reasonable time frame
to show the splash and make it go away.
Miha Markic - RightHand .NET consulting & software development
miha at rthand com

"Ayende Rahien" <Ay****@no.spam> wrote in message
news:eQ*************@TK2MSFTNGP09.phx.gbl...
Excetremely annoying problem, I've an application with a long startup

time.
So I created another form with my logo in it to as a splash screen.
The splash screen is run from another thread and is communicated solely
through static method and Invoke()'s
However, when I close my second form, the first one (main window) is

hiding
under all the windows on the desktop.
If I don't close the splash screen, then everything is fine.


Nov 15 '05 #5
"Jerry Ham" <No*****@Somplace.net> wrote in message
news:Ot**************@TK2MSFTNGP11.phx.gbl...
I had this same problem, but I was able to find a pretty decent workaround
using the API. I use the following:

The splash form is handed the window handle of the main form just as the
main form completes its startup work (a public property on the splash form
accepts this).

Then, the splash form uses:
GetWindowThreadProcessId
followed by "AttachThreadInput" to hook the window message handlers together Then is calls "SetActiveWindow", and then uses AttachThreadInput to put the window message handlers back the way they were.

This has been working fine, and ends up with the main form active.


Thanks a lot!
It now works!!!
Actually, you got me thinking, and then I did a Form1.Activate() after
closing Form2, and it show the main form!
Nov 15 '05 #6
You might be interested in this article:

http://www.codeproject.com/csharp/Pr...lashScreen.asp

Tom Clement
Apptero, Inc.

"Ayende Rahien" <Ay****@no.spam> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
"Jerry Ham" <No*****@Somplace.net> wrote in message
news:Ot**************@TK2MSFTNGP11.phx.gbl...
I had this same problem, but I was able to find a pretty decent workaround using the API. I use the following:

The splash form is handed the window handle of the main form just as the
main form completes its startup work (a public property on the splash form accepts this).

Then, the splash form uses:
GetWindowThreadProcessId
followed by "AttachThreadInput" to hook the window message handlers

together
Then is calls "SetActiveWindow", and then uses AttachThreadInput to put

the
window message handlers back the way they were.

This has been working fine, and ends up with the main form active.


Thanks a lot!
It now works!!!
Actually, you got me thinking, and then I did a Form1.Activate() after
closing Form2, and it show the main form!

Nov 15 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Greg Bacchus | last post by:
Hi, I'm getting an exception that really has me stumped. It's sporadic at best, it's only happened a handful of times. This particular time it happened when the user pressed 'Alt-S' to save the...
15
by: Wiktor Zychla | last post by:
today we've found a critical issue regarding the ListView from Windows.Forms. it was confirmed on several machines with Win2K and XP. here's the problem: create a ListView with about 50000 rows....
4
by: Bilo | last post by:
I have a Windows Forms Class MainGUI I have declared MainGUI maingui; public System.ComponentModel.Container components = new Container(); in the Class I call another class MediaDriver with...
2
by: fperfect13 | last post by:
Hi, I have the folowing exception Exception : System.NullReferenceException: Object reference not set to an instance of an object. 00000019 3:30:48 PM at...
2
by: Raed Sawalha | last post by:
i have a windows form(Main) with listview, when click an item in listview i open other window form (Sub) which generate the selected item from parent window in as treeview items when click any item...
8
by: J.S. | last post by:
I was under the impression that frames could be used in Windows forms in earlier version of VB. However, in VB 2005 Express I don't see any such tool/control. Is SplitContainer used for this...
0
by: gxl034000 | last post by:
Hi, I have been trying to use a .net Forms control in my webpage to open up an application(notepad) on the client. The control works fine when embedded in a windows form, but I keep getting a...
0
by: Jake Montgomery | last post by:
I am using .NET 2.0 forms and running on Windows XP SP2. When I have a tooltip on a combo box, and the combo box is expanded (“dropped down”) using the arrow, it will no longer display it’s...
21
by: Dan Tallent | last post by:
In my application I have a form (Customer) that I want to be able to open multiple copies at once. Within this form I have other forms that can be opened. Example: ZipCode. When the user enters...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.