473,379 Members | 1,170 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,379 software developers and data experts.

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 5041
> 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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Mahesh Devjibhai Dhola [MVP] | last post by:
We are building Chat like application using Forms and as a result our programming is becoming complicated to display messages received on different threads in the chat window (due to STA...
3
by: Scott | last post by:
I have written windows applications using MFC for several years and have frequently used MFC techniques. Now I'm moving to C# .NET WinForm. Mostly C# books describes C# language (sometimes...
6
by: Stephen Brooker | last post by:
Hi all, I've got a basic TCP app that is giving me trouble. I have a separate class that takes care of the TCP connection, and uses the NetworkStreams BeginRead and EndRead with a callback...
4
by: Scott Johnson | last post by:
Hi! Is there a way to "preload" a form using a thread or something else so that my user doesn't have to wait 5 seconds (initializing time) between forms? Some of these forms have tab strips with...
20
by: Peter Oliphant | last post by:
How does one launch multiple forms in an application? Using Photoshop as an example, this application seems to be composed of many 'disjoint' forms. Yet, they all seem somewhat 'active' in...
1
by: Vivek | last post by:
Hi, I am developing a MDI application. I need to track the child forms open so that I can stop a user from opening the multiple instances of the same child form. Now what is the best way of...
11
by: Tony K | last post by:
I have a MDI application. On the menu toolstrip child forms are selected from one of the menus. I don't want to play the disable/enable menu item game. I have selected that open forms are added...
2
by: anon | last post by:
- realtime application has multiple forms. each form is created on a new (gui) thread. this was supposedly for efficiency reasons, i am told. this was done before i took this project. it was done...
21
by: Dan Tallent | last post by:
In my application I have a form (Customer) that I want to be able to open multiple copies at once. Within this form I have other forms that can be opened. Example: ZipCode. When the user enters...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.