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.Collecti ons;
using System.Componen tModel;
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.Componen tModel.Containe r components = null;
private System.Windows. Forms.Button button1;
public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeCompo nent();
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Disp ose();
}
}
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 InitializeCompo nent()
{
this.button1 = new System.Windows. Forms.Button();
this.SuspendLay out();
//
// button1
//
this.button1.Lo cation = new System.Drawing. Point(96, 56);
this.button1.Na me = "button1";
this.button1.Si ze = new System.Drawing. Size(152, 104);
this.button1.Ta bIndex = 0;
this.button1.Te xt = "button1";
this.button1.Cl ick += new System.EventHan dler(this.butto n1_Click);
//
// Form1
//
this.AutoScaleB aseSize = new System.Drawing. Size(5, 13);
this.ClientSize = new System.Drawing. Size(292, 266);
this.Controls.A ddRange(new System.Windows. Forms.Control[] {
this.button1});
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayo ut(false);
}
#endregion
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Form2.Dispaly() ;
System.Threadin g.Thread.Sleep( 5000);
Form2.Vanish();
Application.Run (new Form1());
}
private void button1_Click(o bject sender, System.EventArg s e)
{
}
}
}
// Form2.cs - the splash screen
using System;
using System.Drawing;
using System.Collecti ons;
using System.Componen tModel;
using System.Windows. Forms;
using System.Threadin g;
namespace PrintTest
{
/// <summary>
/// Summary description for Form2.
/// </summary>
public class Form2 : System.Windows. Forms.Form
{
private System.Componen tModel.IContain er components;
private static Form2 That;
private static bool Open;
private System.Windows. Forms.Timer Timer1;
public Form2()
{
//
// Required for Windows Form Designer support
//
InitializeCompo nent();
Timer1.Enabled= true;
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Disp ose();
}
}
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 InitializeCompo nent()
{
this.components = new System.Componen tModel.Containe r();
this.Timer1 = new System.Windows. Forms.Timer(thi s.components);
//
// Timer1
//
this.Timer1.Tic k += new System.EventHan dler(this.Timer 1_Tick);
//
// Form2
//
this.AutoScaleB aseSize = 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(S tart)));
t.Start();
}
public static void Vanish()
{
That.Invoke(new MethodInvoker(V anishInvoked));
}
private static void VanishInvoked()
{
Open = false;
That.Timer1.Ena bled = true;
}
private void Timer1_Tick(obj ect sender, System.EventArg s 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
}
}
}
}
} 6 2534
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.Threadin g.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******** *****@TK2MSFTNG P09.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.
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.co m
"Ayende Rahien" <Ay****@no.spam > wrote in message
news:eQ******** *****@TK2MSFTNG P09.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.Collecti ons;
using System.Componen tModel;
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.Componen tModel.Containe r components = null;
private System.Windows. Forms.Button button1;
public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeCompo nent();
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Disp ose();
}
}
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 InitializeCompo nent()
{
this.button1 = new System.Windows. Forms.Button();
this.SuspendLay out();
//
// button1
//
this.button1.Lo cation = new System.Drawing. Point(96, 56);
this.button1.Na me = "button1";
this.button1.Si ze = new System.Drawing. Size(152, 104);
this.button1.Ta bIndex = 0;
this.button1.Te xt = "button1";
this.button1.Cl ick += new System.EventHan dler(this.butto n1_Click);
//
// Form1
//
this.AutoScaleB aseSize = new System.Drawing. Size(5, 13);
this.ClientSize = new System.Drawing. Size(292, 266);
this.Controls.A ddRange(new System.Windows. Forms.Control[] {
this.button1});
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayo ut(false);
}
#endregion
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Form2.Dispaly() ;
System.Threadin g.Thread.Sleep( 5000);
Form2.Vanish();
Application.Run (new Form1());
} private void button1_Click(o bject sender, System.EventArg s e)
{
}
}
}
// Form2.cs - the splash screen
using System; using System.Drawing; using System.Collecti ons; using System.Componen tModel; using System.Windows. Forms; using System.Threadin g;
namespace PrintTest { /// <summary> /// Summary description for Form2. /// </summary> public class Form2 : System.Windows. Forms.Form { private System.Componen tModel.IContain er components; private static Form2 That; private static bool Open; private System.Windows. Forms.Timer Timer1; public Form2() { // // Required for Windows Form Designer support // InitializeCompo nent(); Timer1.Enabled= true;
}
/// <summary> /// Clean up any resources being used. /// </summary> protected override void Dispose( bool disposing ) { if( disposing ) { if(components != null) { components.Disp ose(); } } 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 InitializeCompo nent() { this.components = new System.Componen tModel.Containe r(); this.Timer1 = new System.Windows. Forms.Timer(thi s.components); // // Timer1 // this.Timer1.Tic k += new System.EventHan dler(this.Timer 1_Tick); // // Form2 // this.AutoScaleB aseSize = 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(S tart))); t.Start(); } public static void Vanish() { That.Invoke(new MethodInvoker(V anishInvoked)); } private static void VanishInvoked() { Open = false; That.Timer1.Ena bled = true; }
private void Timer1_Tick(obj ect sender, System.EventArg s 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 } } } } }
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:
GetWindowThread ProcessId
followed by "AttachThreadIn put" to hook the window message handlers together
Then is calls "SetActiveWindo w", and then uses AttachThreadInp ut 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******** *****@TK2MSFTNG P09.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.Collecti ons;
using System.Componen tModel;
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.Componen tModel.Containe r components = null;
private System.Windows. Forms.Button button1;
public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeCompo nent();
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Disp ose();
}
}
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 InitializeCompo nent()
{
this.button1 = new System.Windows. Forms.Button();
this.SuspendLay out();
//
// button1
//
this.button1.Lo cation = new System.Drawing. Point(96, 56);
this.button1.Na me = "button1";
this.button1.Si ze = new System.Drawing. Size(152, 104);
this.button1.Ta bIndex = 0;
this.button1.Te xt = "button1";
this.button1.Cl ick += new System.EventHan dler(this.butto n1_Click);
//
// Form1
//
this.AutoScaleB aseSize = new System.Drawing. Size(5, 13);
this.ClientSize = new System.Drawing. Size(292, 266);
this.Controls.A ddRange(new System.Windows. Forms.Control[] {
this.button1});
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayo ut(false);
}
#endregion
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Form2.Dispaly() ;
System.Threadin g.Thread.Sleep( 5000);
Form2.Vanish();
Application.Run (new Form1());
} private void button1_Click(o bject sender, System.EventArg s e)
{
}
}
}
// Form2.cs - the splash screen
using System; using System.Drawing; using System.Collecti ons; using System.Componen tModel; using System.Windows. Forms; using System.Threadin g;
namespace PrintTest { /// <summary> /// Summary description for Form2. /// </summary> public class Form2 : System.Windows. Forms.Form { private System.Componen tModel.IContain er components; private static Form2 That; private static bool Open; private System.Windows. Forms.Timer Timer1; public Form2() { // // Required for Windows Form Designer support // InitializeCompo nent(); Timer1.Enabled= true;
}
/// <summary> /// Clean up any resources being used. /// </summary> protected override void Dispose( bool disposing ) { if( disposing ) { if(components != null) { components.Disp ose(); } } 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 InitializeCompo nent() { this.components = new System.Componen tModel.Containe r(); this.Timer1 = new System.Windows. Forms.Timer(thi s.components); // // Timer1 // this.Timer1.Tic k += new System.EventHan dler(this.Timer 1_Tick); // // Form2 // this.AutoScaleB aseSize = 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(S tart))); t.Start(); } public static void Vanish() { That.Invoke(new MethodInvoker(V anishInvoked)); } private static void VanishInvoked() { Open = false; That.Timer1.Ena bled = true; }
private void Timer1_Tick(obj ect sender, System.EventArg s 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 } } } } }
"Miha Markic" <miha at rthand com> wrote in message
news:e7******** ******@TK2MSFTN GP11.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.Threadin g.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******** *****@TK2MSFTNG P09.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.
"Jerry Ham" <No*****@Sompla ce.net> wrote in message
news:Ot******** ******@TK2MSFTN GP11.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: GetWindowThread ProcessId followed by "AttachThreadIn put" to hook the window message handlers
together Then is calls "SetActiveWindo w", and then uses AttachThreadInp ut 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!
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******** ********@TK2MSF TNGP09.phx.gbl. .. "Jerry Ham" <No*****@Sompla ce.net> wrote in message news:Ot******** ******@TK2MSFTN GP11.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: GetWindowThread ProcessId followed by "AttachThreadIn put" to hook the window message handlers together Then is calls "SetActiveWindo w", and then uses AttachThreadInp ut 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!
This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 data that they were entering.
Following is all exception information.
Any thoughts much appreciated.
Cheers
|
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. now use task
manager to see the GDI usage of the process. everything seems normal.
now catch the ListView's scroller and start to move it...
|
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 the Constructor
class MediaDriver
{
|
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
System.Windows.Forms.UnsafeNativeMethods.GetOpenFileName(OPENFILENAME_I
ofn)
00000020 3:30:48 PM at
|
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 in treeview i display the
content item in axWebBrowser, i close the sub form normally when i close the
main the following error is generated
An...
| |
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 purpose in VB 2005?
I want to create a frame in a Windows form because I'd like button clicks on
the left side of the form to change the form elements...
|
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 security exception
when trying to run it from my webpage on my intranet. I have tried
playing with the Code Access Security settings, but I can't get it...
|
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 tooltip, no matter how
long I hover. Even if I move off the control and back it will still not
display its tooltip. If I then go to another control that...
|
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 a zipcode that is unknown
this form will open. I don't want users to modify any of this customers
data until they close the zipcode form. ...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it. ...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
| |
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in...
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...
| |