Friends,
I'm an old C programmer who upgraded to C++ but was never
comfortable with it. I've recently moved to C# and love
it but I obviously am missing some of the subtleties. I
thought the following code would simply expose the Form1
and move on but what actually happens is that the Form1
load hangs while the Worker1 thread goes to sleep. It's
not the Thread.Sleep that's causing the load problem...
any code here executes but the form still hangs. What am
I missing and where can I read up on this, please? Thanks
in advance for your help.
============= program follows =============
using System;
using System.Drawing;
using System.Collecti ons;
using System.Componen tModel;
using System.Windows. Forms;
using System.Data;
using System.Threadin g;
using System.IO;
namespace TestWindowsForm s
{
/// <summary>
/// Summary description for frmTestMain.
///
/// This is a test program to tinker with various
features of Forms based programs.
///
/// </summary>
public class frmTestMain :
System.Windows. Forms.Form
{
private Thread worker1Thread;
private System.Windows. Forms.Button
bnStartWorker;
/// <summary>
/// Required designer variable.
/// </summary>
private System.Componen tModel.Containe r
components = null;
public frmTestMain()
{
//
// Required for Windows Form
Designer support
//
InitializeCompo nent();
//
// TODO: Add any constructor code
after InitializeCompo nent call
//
Thread.CurrentT hread.Name
= "TestMain";
}
/// <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.bnStartWor ker = new
System.Windows. Forms.Button();
this.SuspendLay out();
//
// bnStartWorker
//
this.bnStartWor ker.Location = new
System.Drawing. Point(32, 176);
this.bnStartWor ker.Name
= "bnStartWorker" ;
this.bnStartWor ker.Size = new
System.Drawing. Size(75, 56);
this.bnStartWor ker.TabIndex = 1;
this.bnStartWor ker.Text = "Start
Worker Thread";
this.bnStartWor ker.Click += new
System.EventHan dler(this.bnSta rtWorker_Click) ;
//
// frmTestMain
//
this.AutoScaleB aseSize = new
System.Drawing. Size(5, 13);
this.ClientSize = new
System.Drawing. Size(292, 273);
this.Controls.A dd
(this.bnStartWo rker);
this.Name = "frmTestMai n";
this.Text = "Testing Windows Forms
(Main)";
this.Closing += new
System.Componen tModel.CancelEv entHandler
(this.TestMain_ Closing);
this.ResumeLayo ut(false);
}
#endregion
/// <summary>
/// The main entry point for the
application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run (new frmTestMain());
}
private void TestMain_Closin g(object
sender, System.Componen tModel.CancelEv entArgs e)
{
// verify the closure
DialogResult r;
r=MessageBox.Sh ow("This will exit
the Test Windows program.\r\n\nD o you really wish to
exit?","Exit Test
Windows",Messag eBoxButtons.Yes No,MessageBoxIc on.Question);
if (r==DialogResul t.No)
e.Cancel=true;
else
{
worker1Thread.A bort();
worker1Thread.J oin();
System.Environm ent.Exit
(System.Environ ment.ExitCode);
}
}
private void aThread1()
{
frmSubForm1 Form1 = new frmSubForm1
();
try
{
Form1.Visible = true;
while (true) Thread.Sleep
(0); // do nothing
}
catch (ThreadAbortExc eption)
{
// clean up thread
resources
Form1.Dispose() ;
}
}
private void bnStartWorker_C lick(object
sender, System.EventArg s e)
{
worker1Thread = new Thread(new
ThreadStart(aTh read1));
worker1Thread.N ame="Worker1";
worker1Thread.S tart();
}
}
}
I'm an old C programmer who upgraded to C++ but was never
comfortable with it. I've recently moved to C# and love
it but I obviously am missing some of the subtleties. I
thought the following code would simply expose the Form1
and move on but what actually happens is that the Form1
load hangs while the Worker1 thread goes to sleep. It's
not the Thread.Sleep that's causing the load problem...
any code here executes but the form still hangs. What am
I missing and where can I read up on this, please? Thanks
in advance for your help.
============= program follows =============
using System;
using System.Drawing;
using System.Collecti ons;
using System.Componen tModel;
using System.Windows. Forms;
using System.Data;
using System.Threadin g;
using System.IO;
namespace TestWindowsForm s
{
/// <summary>
/// Summary description for frmTestMain.
///
/// This is a test program to tinker with various
features of Forms based programs.
///
/// </summary>
public class frmTestMain :
System.Windows. Forms.Form
{
private Thread worker1Thread;
private System.Windows. Forms.Button
bnStartWorker;
/// <summary>
/// Required designer variable.
/// </summary>
private System.Componen tModel.Containe r
components = null;
public frmTestMain()
{
//
// Required for Windows Form
Designer support
//
InitializeCompo nent();
//
// TODO: Add any constructor code
after InitializeCompo nent call
//
Thread.CurrentT hread.Name
= "TestMain";
}
/// <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.bnStartWor ker = new
System.Windows. Forms.Button();
this.SuspendLay out();
//
// bnStartWorker
//
this.bnStartWor ker.Location = new
System.Drawing. Point(32, 176);
this.bnStartWor ker.Name
= "bnStartWorker" ;
this.bnStartWor ker.Size = new
System.Drawing. Size(75, 56);
this.bnStartWor ker.TabIndex = 1;
this.bnStartWor ker.Text = "Start
Worker Thread";
this.bnStartWor ker.Click += new
System.EventHan dler(this.bnSta rtWorker_Click) ;
//
// frmTestMain
//
this.AutoScaleB aseSize = new
System.Drawing. Size(5, 13);
this.ClientSize = new
System.Drawing. Size(292, 273);
this.Controls.A dd
(this.bnStartWo rker);
this.Name = "frmTestMai n";
this.Text = "Testing Windows Forms
(Main)";
this.Closing += new
System.Componen tModel.CancelEv entHandler
(this.TestMain_ Closing);
this.ResumeLayo ut(false);
}
#endregion
/// <summary>
/// The main entry point for the
application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run (new frmTestMain());
}
private void TestMain_Closin g(object
sender, System.Componen tModel.CancelEv entArgs e)
{
// verify the closure
DialogResult r;
r=MessageBox.Sh ow("This will exit
the Test Windows program.\r\n\nD o you really wish to
exit?","Exit Test
Windows",Messag eBoxButtons.Yes No,MessageBoxIc on.Question);
if (r==DialogResul t.No)
e.Cancel=true;
else
{
worker1Thread.A bort();
worker1Thread.J oin();
System.Environm ent.Exit
(System.Environ ment.ExitCode);
}
}
private void aThread1()
{
frmSubForm1 Form1 = new frmSubForm1
();
try
{
Form1.Visible = true;
while (true) Thread.Sleep
(0); // do nothing
}
catch (ThreadAbortExc eption)
{
// clean up thread
resources
Form1.Dispose() ;
}
}
private void bnStartWorker_C lick(object
sender, System.EventArg s e)
{
worker1Thread = new Thread(new
ThreadStart(aTh read1));
worker1Thread.N ame="Worker1";
worker1Thread.S tart();
}
}
}
Comment