470,591 Members | 2,247 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,591 developers. It's quick & easy.

Opening forms when using multiple threads

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.Qu estion);
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();
}

}
}

Nov 15 '05 #1
1 4856
> 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/de...us/dnforms/htm
l/winforms06112002.asp
Nov 15 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

8 posts views Thread by Mahesh Devjibhai Dhola [MVP] | last post: by
3 posts views Thread by Scott | last post: by
6 posts views Thread by Stephen Brooker | last post: by
4 posts views Thread by Scott Johnson | last post: by
2 posts views Thread by anon | last post: by
21 posts views Thread by Dan Tallent | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.