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 ApplicationLoader.cs and MainApplicationForm.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 ApplicationLoader.cs------------
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Threading;
namespace startupscreen
{
public class statusInformationForm : System.Windows.Forms.Form
{
private System.Windows.Forms.Label statusInformation;
private System.Windows.Forms.Label statusTitleLabel;
private System.ComponentModel.Container components = null;
public statusInformationForm()
{
InitializeComponent();
}
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
private string theStatusInfo = String.Empty;
public string StatusInfo
{
set
{
theStatusInfo = value;
updateStatusText();
}
get
{
return theStatusInfo;
}
}
public void updateStatusText()
{
try
{
if (this.InvokeRequired)
{
this.Invoke(new MethodInvoker(this.updateStatusText));
return;
}
statusInformation.Text = theStatusInfo;
}
catch (Exception e)
{
}
}
#region Windows Form Designer generated code
private void InitializeComponent()
{
this.statusInformation = new System.Windows.Forms.Label();
this.statusTitleLabel = new System.Windows.Forms.Label();
this.SuspendLayout();
//
// statusInformation
//
this.statusInformation.Location = new System.Drawing.Point(80, 40);
this.statusInformation.Name = "statusInformation";
this.statusInformation.Size = new System.Drawing.Size(200, 16);
this.statusInformation.TabIndex = 1;
//
// statusTitleLabel
//
this.statusTitleLabel.Location = new System.Drawing.Point(16, 40);
this.statusTitleLabel.Name = "statusTitleLabel";
this.statusTitleLabel.Size = new System.Drawing.Size(48, 16);
this.statusTitleLabel.TabIndex = 2;
this.statusTitleLabel.Text = "Status:";
//
// statusInformationForm
//
this.AutoScaleBaseSize = 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.Add(this.statusTitleLabel);
this.Controls.Add(this.statusInformation);
this.FormBorderStyle =
System.Windows.Forms.FormBorderStyle.FixedSingle;
this.Name = "statusInformationForm";
this.StartPosition =
System.Windows.Forms.FormStartPosition.CenterScree n;
this.ResumeLayout(false);
}
#endregion
}
public class ApplicationLoader
{
[STAThread]
static void Main(string[] args)
{
MainApplicationForm mainApplicationForm = new
MainApplicationForm();
Application.Run(mainApplicationForm);
}
}
public class statusInformationScreen
{
private static statusInformationForm theStatusInformationForm =
null;
private static Thread statusInformationThread = null;
private static void ShowThread()
{
theStatusInformationForm = new statusInformationForm();
Application.Run(theStatusInformationForm);
}
public static void Show()
{
if (statusInformationThread != null)
return;
statusInformationThread = new Thread(new
ThreadStart(statusInformationScreen.ShowThread));
statusInformationThread.IsBackground = true;
statusInformationThread.ApartmentState = ApartmentState.STA;
statusInformationThread.Start();
}
public static void Close()
{
if (statusInformationThread == null || theStatusInformationForm ==
null)
{
statusInformationThread = null;
theStatusInformationForm = null;
return;
}
try
{
theStatusInformationForm.Invoke(new
MethodInvoker(theStatusInformationForm.Close));
}
catch (Exception)
{
}
statusInformationThread = null;
theStatusInformationForm = null;
}
public static string Status
{
set
{
if (theStatusInformationForm == null)
{
return;
}
theStatusInformationForm.StatusInfo = value;
}
get
{
if (theStatusInformationForm == null)
{
throw new InvalidOperationException("Form not visible?");
}
return theStatusInformationForm.StatusInfo;
}
}
}
}
------End ApplicationLoader.cs------------
------Start MainApplicationForm.cs--------
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Threading;
namespace startupscreen
{
public class MainApplicationForm : System.Windows.Forms.Form
{
private System.Windows.Forms.Button Button;
private System.ComponentModel.Container components = null;
public MainApplicationForm()
{
InitializeComponent();
statusInformationScreen.Show();
//
// This status information is not ALWAYS displayed
//
statusInformationScreen.Status = "Startup Point 1";
System.Threading.Thread.Sleep(2000);
statusInformationScreen.Status = "Startup Point 2";
System.Threading.Thread.Sleep(2000);
statusInformationScreen.Status = "Startup Point 3";
System.Threading.Thread.Sleep(2000);
statusInformationScreen.Close();
}
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
private void InitializeComponent()
{
this.Button = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// Button
//
this.Button.Location = new System.Drawing.Point(72, 88);
this.Button.Name = "Button";
this.Button.Size = new System.Drawing.Size(136, 96);
this.Button.TabIndex = 0;
this.Button.Text = "Push Me";
this.Button.Click += new System.EventHandler(this.button_Click);
//
// MainApplicationForm
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(292, 266);
this.Controls.Add(this.Button);
this.Name = "MainApplicationForm";
this.Text = "My Form";
this.ResumeLayout(false);
}
#endregion
protected override void OnLoad(System.EventArgs e)
{
this.Activate();
}
private void button_Click(object sender, System.EventArgs e)
{
statusInformationScreen.Show();
//
// This status information is not ALWAYS displayed
//
statusInformationScreen.Status = "Running Point 1";
System.Threading.Thread.Sleep(2000);
statusInformationScreen.Status = "Running Point 2";
System.Threading.Thread.Sleep(2000);
statusInformationScreen.Status = "Running Point 3";
System.Threading.Thread.Sleep(2000);
statusInformationScreen.Close();
}
}
}
------End MainApplicationForm.cs--------