Form and Network Listener
-
I am a newbie to C# so please excuse if this problem is really something simple. I am trying to create an app that sets a window to maximized state after it recieves a certain command over a TCP port. I can get the tcp listener to work but I cant get the form to display. The form never even shows up. I only get the runtime error "Object reference not set to an instance of an object" after I enter the command via the network to set the form to maximized. I think the problem has to do with my calling a method from within the constructor when the constructor has not yet finished. I have attached my code if anyone is willing to look and suggest where I might find where I am going wrong. using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Net; using System.Net.Sockets; namespace networkScreenBlanker { /// /// Summary description for Form1. /// public class Form1 : System.Windows.Forms.Form { /// /// Required designer variable. /// private System.ComponentModel.Container components = null; public Form1() { // // Required for Windows Form Designer support // InitializeComponent(); this.Run(); // // TODO: Add any constructor code after InitializeComponent call // } /// /// Clean up any resources being used. /// protected override void Dispose( bool disposing ) { if( disposing ) { if(components != null) { components.Dispose(); } } base.Dispose( disposing ); } #region Windows Form Designer generated code /// /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// private void InitializeComponent() { // // Form1 // this.AutoScaleBaseSize = new System.Drawing.Size(5, 13); this.ClientSize = new System.Drawing.Size(292, 266); this.Name = "Form1"; this.Text = "Form1"; this.WindowState = System.Windows.Forms.FormWindowState.Minimized; } #endregion private void Run() { IPAddress localAddr = IPAddress.Parse("127.0.0.1"); TcpListener tcpListener = new TcpListener(localAddr, 65000); tcpListener.Start(); for(;;) { Socket socketForClient = tcpListener.AcceptSocket(); if (socketForClient.Connected) { //Console.WriteLine("Client connected"); //
-
I am a newbie to C# so please excuse if this problem is really something simple. I am trying to create an app that sets a window to maximized state after it recieves a certain command over a TCP port. I can get the tcp listener to work but I cant get the form to display. The form never even shows up. I only get the runtime error "Object reference not set to an instance of an object" after I enter the command via the network to set the form to maximized. I think the problem has to do with my calling a method from within the constructor when the constructor has not yet finished. I have attached my code if anyone is willing to look and suggest where I might find where I am going wrong. using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Net; using System.Net.Sockets; namespace networkScreenBlanker { /// /// Summary description for Form1. /// public class Form1 : System.Windows.Forms.Form { /// /// Required designer variable. /// private System.ComponentModel.Container components = null; public Form1() { // // Required for Windows Form Designer support // InitializeComponent(); this.Run(); // // TODO: Add any constructor code after InitializeComponent call // } /// /// Clean up any resources being used. /// protected override void Dispose( bool disposing ) { if( disposing ) { if(components != null) { components.Dispose(); } } base.Dispose( disposing ); } #region Windows Form Designer generated code /// /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// private void InitializeComponent() { // // Form1 // this.AutoScaleBaseSize = new System.Drawing.Size(5, 13); this.ClientSize = new System.Drawing.Size(292, 266); this.Name = "Form1"; this.Text = "Form1"; this.WindowState = System.Windows.Forms.FormWindowState.Minimized; } #endregion private void Run() { IPAddress localAddr = IPAddress.Parse("127.0.0.1"); TcpListener tcpListener = new TcpListener(localAddr, 65000); tcpListener.Start(); for(;;) { Socket socketForClient = tcpListener.AcceptSocket(); if (socketForClient.Connected) { //Console.WriteLine("Client connected"); //
Yes you are correct. The code is halting within Form1.Run() (called from the constructor) so execution never reaches Application.Run(new Form1()), which is the reason you never see the form open. To acheive the result you require can be acheived by running the Form1.Run() method as a seperate worker thread which can be achieved something like:-
public class Form1 : System.Windows.Forms.Form { private Thread trdTcpActivityListener = null; public Form1() { InitializeComponent(); // Initialize seperate thread and start it trdTcpActivityListener = new Thread(new ThreadStart(this.Run)); trdTcpActivityListener.Start(); // Instead of - this.Run(); } public void Run() { TcpListener tcpListener = new TcpListener(IPAddress.Any, 65000); tcpListener.Start(); while (true) { // Use .Pending() to determine if there is any connections if (tcpListener.Pending()) { Socket socketForClient = tcpListener.AcceptSocket(); socketForClient.Close(); // Maximize the form this.WindowState = FormWindowState.Maximized; } // Make sure the while loop doesn't hog the CPU Thread.Sleep(30); } } }
Now whilst the worker thread waits for a TCP connection, the main thread is allowed to continue, the constructor completes and the form opens (minimized). Ill leave it up to you to work out when you should stop the worker thread.
-
Yes you are correct. The code is halting within Form1.Run() (called from the constructor) so execution never reaches Application.Run(new Form1()), which is the reason you never see the form open. To acheive the result you require can be acheived by running the Form1.Run() method as a seperate worker thread which can be achieved something like:-
public class Form1 : System.Windows.Forms.Form { private Thread trdTcpActivityListener = null; public Form1() { InitializeComponent(); // Initialize seperate thread and start it trdTcpActivityListener = new Thread(new ThreadStart(this.Run)); trdTcpActivityListener.Start(); // Instead of - this.Run(); } public void Run() { TcpListener tcpListener = new TcpListener(IPAddress.Any, 65000); tcpListener.Start(); while (true) { // Use .Pending() to determine if there is any connections if (tcpListener.Pending()) { Socket socketForClient = tcpListener.AcceptSocket(); socketForClient.Close(); // Maximize the form this.WindowState = FormWindowState.Maximized; } // Make sure the while loop doesn't hog the CPU Thread.Sleep(30); } } }
Now whilst the worker thread waits for a TCP connection, the main thread is allowed to continue, the constructor completes and the form opens (minimized). Ill leave it up to you to work out when you should stop the worker thread.
Thanks for the suggestion. This worked. Now to read that chapter on threading..... Jackson