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

C#

 
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 
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 
I agree with you: the Socket class has much more functionality. If you can bear with me, though, I modified my last example with the TcpLiseners to handle actual messages passed into the child forms.

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( IPAddress.Parse( "127.0.0.1" ), 5555 );
			listener = new TcpListener( endPoint );
			listener.Start();
			while( true )
			{
				try
				{
					TcpClient client = listener.AcceptTcpClient();
					this.Invoke( new HandleNewConnection( OpenChildForm ), new object[] { client } );
				}
				catch( Exception ) {}
			}
		}

		private delegate void HandleNewConnection( TcpClient client );

		private void OpenChildForm( TcpClient client )
		{
			ChildForm cf = new ChildForm( client );
			cf.Show();
		}

		private void b_Click( object sender, EventArgs e )
		{
			clients[ clientIndex ] = new TcpClient( "127.0.0.1", 5555 );
			byte[] b = System.Text.Encoding.ASCII.GetBytes( message );
			clients[ clientIndex ].GetStream().Write( b, 0, b.Length );
			clientIndex++;
			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() );
		}

		private string message = "It was many and many a year ago in a kingdom by the sea\n" +
			                     "Where a maiden there lived whom you may know by the name of Annabel Lee.\n" +
								 "And, this maiden, she lived with no other thought than to love and be loved by me.";
	}

	public class ChildForm : Form
	{
		public ChildForm( TcpClient client ) : base()
		{
			Label l = new Label();
			l.Dock = DockStyle.Fill;
			Controls.Add( l );
			c = client;
			Closing += new System.ComponentModel.CancelEventHandler( ChildForm_Closing );
			buffer = new byte[ 256 ];
			Thread t = new Thread( new ThreadStart( HandleConnection ) );
			t.Start();
		}

		private void HandleConnection()
		{
			string s = null;
			NetworkStream cnxStream = c.GetStream();
			int len;
			try
			{
				while( ( len = cnxStream.Read( buffer, 0, 256 ) ) > -1 )
				{
					Controls[ 0 ].Text += System.Text.Encoding.ASCII.GetString( buffer, 0, len );
				}
			}
			catch( Exception ){}
		}

		private void ChildForm_Closing(object sender, System.ComponentModel.CancelEventArgs e)
		{
			c.Close();
		}

		private byte[] buffer;
		private TcpClient c;
	}
}


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

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 
AnswerRe: sql statement problem Pin
jonny511-Nov-05 0:31
jonny511-Nov-05 0:31 
Questionwhat is the complete code for downloading a file using FTP through ASP.NET/C# Pin
v.k.s10-Nov-05 17:26
v.k.s10-Nov-05 17:26 
AnswerRe: what is the complete code for downloading a file using FTP through ASP.NET/C# Pin
Ravi Bhavnani11-Nov-05 5:51
professionalRavi Bhavnani11-Nov-05 5:51 

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.