Opening forms when using multiple threads

  • Thread starter Thread starter Alasdair
  • Start date Start date
A

Alasdair

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.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Threading;
using System.IO;

namespace TestWindowsForms
{
/// <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.ComponentModel.Container
components = null;

public frmTestMain()
{
//
// Required for Windows Form
Designer support
//
InitializeComponent();

//
// TODO: Add any constructor code
after InitializeComponent call
//
Thread.CurrentThread.Name
= "TestMain";
}

/// <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.bnStartWorker = new
System.Windows.Forms.Button();
this.SuspendLayout();
//
// bnStartWorker
//
this.bnStartWorker.Location = new
System.Drawing.Point(32, 176);
this.bnStartWorker.Name
= "bnStartWorker";
this.bnStartWorker.Size = new
System.Drawing.Size(75, 56);
this.bnStartWorker.TabIndex = 1;
this.bnStartWorker.Text = "Start
Worker Thread";
this.bnStartWorker.Click += new
System.EventHandler(this.bnStartWorker_Click);
//
// frmTestMain
//
this.AutoScaleBaseSize = new
System.Drawing.Size(5, 13);
this.ClientSize = new
System.Drawing.Size(292, 273);
this.Controls.Add
(this.bnStartWorker);
this.Name = "frmTestMain";
this.Text = "Testing Windows Forms
(Main)";
this.Closing += new
System.ComponentModel.CancelEventHandler
(this.TestMain_Closing);
this.ResumeLayout(false);

}
#endregion

/// <summary>
/// The main entry point for the
application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new frmTestMain());
}

private void TestMain_Closing(object
sender, System.ComponentModel.CancelEventArgs e)
{
// verify the closure
DialogResult r;
r=MessageBox.Show("This will exit
the Test Windows program.\r\n\nDo you really wish to
exit?","Exit Test
Windows",MessageBoxButtons.YesNo,MessageBoxIcon.Question);
if (r==DialogResult.No)
e.Cancel=true;
else
{
worker1Thread.Abort();
worker1Thread.Join();
System.Environment.Exit
(System.Environment.ExitCode);
}
}

private void aThread1()
{
frmSubForm1 Form1 = new frmSubForm1
();

try
{
Form1.Visible = true;
while (true) Thread.Sleep
(0); // do nothing
}
catch (ThreadAbortException)
{
// clean up thread
resources
Form1.Dispose();
}
}

private void bnStartWorker_Click(object
sender, System.EventArgs e)
{
worker1Thread = new Thread(new
ThreadStart(aThread1));
worker1Thread.Name="Worker1";
worker1Thread.Start();
}

}
}
 
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.

read carefully

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnforms/htm
l/winforms06112002.asp
 
Back
Top