Click here to Skip to main content
15,890,438 members
Home / Discussions / C#
   

C#

 
QuestionA True Global Class? Pin
redfish3410-Nov-05 20:24
redfish3410-Nov-05 20:24 
AnswerRe: A True Global Class? Pin
Craig G Fraser10-Nov-05 22:48
Craig G Fraser10-Nov-05 22:48 
GeneralRe: A True Global Class? Pin
redfish3415-Nov-05 19:24
redfish3415-Nov-05 19:24 
QuestionCreating forms on different threads Pin
kourvoisier10-Nov-05 19:57
kourvoisier10-Nov-05 19:57 
AnswerRe: Creating forms on different threads Pin
jonny511-Nov-05 0:23
jonny511-Nov-05 0:23 
GeneralRe: Creating forms on different threads Pin
kourvoisier11-Nov-05 3:45
kourvoisier11-Nov-05 3:45 
GeneralRe: Creating forms on different threads Pin
kourvoisier11-Nov-05 8:54
kourvoisier11-Nov-05 8:54 
GeneralRe: Creating forms on different threads Pin
Curtis Schlak.14-Nov-05 4:04
Curtis Schlak.14-Nov-05 4:04 
Hi there. I hope that this code helps. It starts a thread in the main form's constructor and, then, when a new connection gets accepted, opens a new child form.

C#
using System;
using System.Drawing;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using System.Windows.Forms;

namespace FormsSpawnedByThreads
{
	public class MainForm : Form
	{
		public MainForm() : base()
		{
			Text = "Spawn Some Forms";
			Height = 150;
			Width = 300;
			Button b = new Button();
			b.Text = "Connect";
			b.Top = ( Height - b.Height ) / 2 - ( b.Height / 2 );
			b.Left = ( Width- b.Width ) / 2;
			b.Click += new EventHandler( b_Click );
			Controls.Add( b );
			Thread t = new Thread( new ThreadStart( ListenNewConnections ) );
			t.IsBackground = true;
			t.Name = "Main Listener";
			t.Start();
			clients = new TcpClient[ 5 ];
			Closing += new System.ComponentModel.CancelEventHandler( MainForm_Closing );
		}

		private void ListenNewConnections()
		{
			IPEndPoint endPoint = new IPEndPoint( 0x7F000001, 5050 );
			listener = new TcpListener( 5050 );
			listener.Start();
			while( true )
			{
				listener.AcceptTcpClient();
				ChildForm cf = new ChildForm();
				cf.Show();
			}
		}

		private void b_Click( object sender, EventArgs e )
		{
			clients[ clientIndex++ ] = new TcpClient( "localhost", 5050 );
			if( clientIndex == 5 )
			{
				Controls[ 0 ].Enabled = false;
			}
		}

		private void MainForm_Closing(object sender, System.ComponentModel.CancelEventArgs e)
		{
			if( listener != null )
			{
				listener.Stop();
			}
		}

		private TcpClient[] clients;
		private int clientIndex;
		private TcpListener listener;

		[STAThread]
		public static void Main()
		{
			Application.Run( new MainForm() );
		}
	}

	public class ChildForm : Form
	{
		public ChildForm() : base()
		{
			
		}
	}
}


"we must lose precision to make significant
      statements about complex systems."
              -deKorvin on uncertainty

GeneralRe: Creating forms on different threads EVEN BETTER Pin
Curtis Schlak.14-Nov-05 4:42
Curtis Schlak.14-Nov-05 4:42 
GeneralRe: Creating forms on different threads EVEN BETTER Pin
kourvoisier14-Nov-05 5:16
kourvoisier14-Nov-05 5:16 
GeneralRe: Creating forms on different threads EVEN BETTER Pin
Curtis Schlak.14-Nov-05 5:51
Curtis Schlak.14-Nov-05 5:51 
GeneralRe: Creating forms on different threads EVEN BETTER Pin
kourvoisier14-Nov-05 12:18
kourvoisier14-Nov-05 12:18 
QuestionSet form's owner to a window outside the application? Pin
MogobuTheFool10-Nov-05 19:45
MogobuTheFool10-Nov-05 19:45 
AnswerRe: Set form's owner to a window outside the application? Pin
Dave Kreskowiak11-Nov-05 3:57
mveDave Kreskowiak11-Nov-05 3:57 
GeneralRe: Set form's owner to a window outside the application? Pin
MogobuTheFool7-Jul-06 8:32
MogobuTheFool7-Jul-06 8:32 
GeneralRe: Set form's owner to a window outside the application? Pin
Dave Kreskowiak7-Jul-06 10:16
mveDave Kreskowiak7-Jul-06 10:16 
GeneralRe: Set form's owner to a window outside the application? Pin
MogobuTheFool8-Jul-06 4:18
MogobuTheFool8-Jul-06 4:18 
Questionsetting the date format of the calendar in a datetimepicker Pin
microsoc10-Nov-05 19:28
microsoc10-Nov-05 19:28 
AnswerRe: setting the date format of the calendar in a datetimepicker Pin
krario10-Nov-05 20:07
krario10-Nov-05 20:07 
GeneralRe: setting the date format of the calendar in a datetimepicker Pin
microsoc10-Nov-05 20:20
microsoc10-Nov-05 20:20 
QuestionStatic and Dynaminc Assemblies ? Pin
webC#10-Nov-05 19:08
webC#10-Nov-05 19:08 
AnswerRe: Static and Dynaminc Assemblies ? Pin
J4amieC10-Nov-05 21:51
J4amieC10-Nov-05 21:51 
QuestionData Encryption in MySql Pin
Prashant Gadhave10-Nov-05 19:06
Prashant Gadhave10-Nov-05 19:06 
QuestionCapture Voice using DirectSound Pin
pakFari10-Nov-05 18:35
pakFari10-Nov-05 18:35 
Questionsql statement problem Pin
ppp00110-Nov-05 18:32
ppp00110-Nov-05 18:32 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.