Dear Group,
I am attempting to write a "splash" and "status" Form using a second
thread.
I wish to use this Form to display status information to the user when
I do CPU intensive work in my GUI during startup. I wish to also use
the same Form after startup as well if I do something CPU intensive.
I have developed a multi-threaded application however the very first
status update does not always display. It sometimes appears – which I
am assuming is to do with the interaction of my two threads.
Could anyone comment on what I have missed that causes the "Startup
Point 1" and "Running Point 1" status labels to sometimes display and
others not.
I have included the following cutdown example of what I am doing.
There are two files ApplicationLoad er.cs and MainApplication Form.cs.
Run the application to see the startup screen and press the button to
see the running version. Note the first status information label
shown.
Any comments would be most appreciated.
Thanks
Stuart
------Start ApplicationLoad er.cs------------
using System;
using System.Drawing;
using System.Collecti ons;
using System.Componen tModel;
using System.Windows. Forms;
using System.Threadin g;
namespace startupscreen
{
public class statusInformati onForm : System.Windows. Forms.Form
{
private System.Windows. Forms.Label statusInformati on;
private System.Windows. Forms.Label statusTitleLabe l;
private System.Componen tModel.Containe r components = null;
public statusInformati onForm()
{
InitializeCompo nent();
}
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Disp ose();
}
}
base.Dispose( disposing );
}
private string theStatusInfo = String.Empty;
public string StatusInfo
{
set
{
theStatusInfo = value;
updateStatusTex t();
}
get
{
return theStatusInfo;
}
}
public void updateStatusTex t()
{
try
{
if (this.InvokeReq uired)
{
this.Invoke(new MethodInvoker(t his.updateStatu sText));
return;
}
statusInformati on.Text = theStatusInfo;
}
catch (Exception e)
{
}
}
#region Windows Form Designer generated code
private void InitializeCompo nent()
{
this.statusInfo rmation = new System.Windows. Forms.Label();
this.statusTitl eLabel = new System.Windows. Forms.Label();
this.SuspendLay out();
//
// statusInformati on
//
this.statusInfo rmation.Locatio n = new System.Drawing. Point(80, 40);
this.statusInfo rmation.Name = "statusInformat ion";
this.statusInfo rmation.Size = new System.Drawing. Size(200, 16);
this.statusInfo rmation.TabInde x = 1;
//
// statusTitleLabe l
//
this.statusTitl eLabel.Location = new System.Drawing. Point(16, 40);
this.statusTitl eLabel.Name = "statusTitleLab el";
this.statusTitl eLabel.Size = new System.Drawing. Size(48, 16);
this.statusTitl eLabel.TabIndex = 2;
this.statusTitl eLabel.Text = "Status:";
//
// statusInformati onForm
//
this.AutoScaleB aseSize = new System.Drawing. Size(5, 13);
this.BackColor = System.Drawing. Color.White;
this.ClientSize = new System.Drawing. Size(300, 94);
this.ControlBox = false;
this.Controls.A dd(this.statusT itleLabel);
this.Controls.A dd(this.statusI nformation);
this.FormBorder Style =
System.Windows. Forms.FormBorde rStyle.FixedSin gle;
this.Name = "statusInformat ionForm";
this.StartPosit ion =
System.Windows. Forms.FormStart Position.Center Screen;
this.ResumeLayo ut(false);
}
#endregion
}
public class ApplicationLoad er
{
[STAThread]
static void Main(string[] args)
{
MainApplication Form mainApplication Form = new
MainApplication Form();
Application.Run (mainApplicatio nForm);
}
}
public class statusInformati onScreen
{
private static statusInformati onForm theStatusInform ationForm =
null;
private static Thread statusInformati onThread = null;
private static void ShowThread()
{
theStatusInform ationForm = new statusInformati onForm();
Application.Run (theStatusInfor mationForm);
}
public static void Show()
{
if (statusInformat ionThread != null)
return;
statusInformati onThread = new Thread(new
ThreadStart(sta tusInformationS creen.ShowThrea d));
statusInformati onThread.IsBack ground = true;
statusInformati onThread.Apartm entState = ApartmentState. STA;
statusInformati onThread.Start( );
}
public static void Close()
{
if (statusInformat ionThread == null || theStatusInform ationForm ==
null)
{
statusInformati onThread = null;
theStatusInform ationForm = null;
return;
}
try
{
theStatusInform ationForm.Invok e(new
MethodInvoker(t heStatusInforma tionForm.Close) );
}
catch (Exception)
{
}
statusInformati onThread = null;
theStatusInform ationForm = null;
}
public static string Status
{
set
{
if (theStatusInfor mationForm == null)
{
return;
}
theStatusInform ationForm.Statu sInfo = value;
}
get
{
if (theStatusInfor mationForm == null)
{
throw new InvalidOperatio nException("For m not visible?");
}
return theStatusInform ationForm.Statu sInfo;
}
}
}
}
------End ApplicationLoad er.cs------------
------Start MainApplication Form.cs--------
using System;
using System.Drawing;
using System.Collecti ons;
using System.Componen tModel;
using System.Windows. Forms;
using System.Data;
using System.Threadin g;
namespace startupscreen
{
public class MainApplication Form : System.Windows. Forms.Form
{
private System.Windows. Forms.Button Button;
private System.Componen tModel.Containe r components = null;
public MainApplication Form()
{
InitializeCompo nent();
statusInformati onScreen.Show() ;
//
// This status information is not ALWAYS displayed
//
statusInformati onScreen.Status = "Startup Point 1";
System.Threadin g.Thread.Sleep( 2000);
statusInformati onScreen.Status = "Startup Point 2";
System.Threadin g.Thread.Sleep( 2000);
statusInformati onScreen.Status = "Startup Point 3";
System.Threadin g.Thread.Sleep( 2000);
statusInformati onScreen.Close( );
}
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Disp ose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
private void InitializeCompo nent()
{
this.Button = new System.Windows. Forms.Button();
this.SuspendLay out();
//
// Button
//
this.Button.Loc ation = new System.Drawing. Point(72, 88);
this.Button.Nam e = "Button";
this.Button.Siz e = new System.Drawing. Size(136, 96);
this.Button.Tab Index = 0;
this.Button.Tex t = "Push Me";
this.Button.Cli ck += new System.EventHan dler(this.butto n_Click);
//
// MainApplication Form
//
this.AutoScaleB aseSize = new System.Drawing. Size(5, 13);
this.ClientSize = new System.Drawing. Size(292, 266);
this.Controls.A dd(this.Button) ;
this.Name = "MainApplicatio nForm";
this.Text = "My Form";
this.ResumeLayo ut(false);
}
#endregion
protected override void OnLoad(System.E ventArgs e)
{
this.Activate() ;
}
private void button_Click(ob ject sender, System.EventArg s e)
{
statusInformati onScreen.Show() ;
//
// This status information is not ALWAYS displayed
//
statusInformati onScreen.Status = "Running Point 1";
System.Threadin g.Thread.Sleep( 2000);
statusInformati onScreen.Status = "Running Point 2";
System.Threadin g.Thread.Sleep( 2000);
statusInformati onScreen.Status = "Running Point 3";
System.Threadin g.Thread.Sleep( 2000);
statusInformati onScreen.Close( );
}
}
}
------End MainApplication Form.cs--------