Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Form and Network Listener

Form and Network Listener

Scheduled Pinned Locked Moved C#
csharphelpgraphicssysadmindocker
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    Lost User
    wrote on last edited by
    #1

    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"); //

    A 1 Reply Last reply
    0
    • L Lost User

      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"); //

      A Offline
      A Offline
      Allan Eagle
      wrote on last edited by
      #2

      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.

      U 1 Reply Last reply
      0
      • A Allan Eagle

        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.

        U Offline
        U Offline
        User 1452207
        wrote on last edited by
        #3

        Thanks for the suggestion. This worked. Now to read that chapter on threading..... Jackson

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups