Search code examples
c#debuggingconsoledevtools

C# Dynamic Console for debugging possible?


I'm creating a program and i would really like to create a quick dev window into which i can type comands. I will try pseudo code what i'm trying to say.

Console
 User Types "Add player"
 Takes the string and searches a case statement which says
 Carry out these action on Form1

Is this possible to create a console which i can manipulate my C# Form program with?

The only answer i need is "Yes, to spawn a console do this........" i can work the rest out for myself. Or "no"

Thank you for your time.


Solution

  • Yes, you can. Create a console application, add the necessary references and forms, then show your main form at the beginning. With this code you can change the content of a label on a form:

    Program.cs:

    static void Main( string[ ] args ) {
        var f = new Form1( );
    
        var t = new Thread( delegate( object form ) {
            System.Windows.Forms.Application.Run( form as Form1 );
        } );
    
        t.Start( f );
    
        while ( true )
            f.SetText( Console.ReadLine( ) );
    
        t.Join( );
    }
    

    Form1.cs:

    public partial class Form1 : Form {
        public Form1( ) {
            InitializeComponent( );
            CheckForIllegalCrossThreadCalls = false;   // Note!!
        }
    
        public void SetText( string text ) {
            label1.Text = text;
        }
    }
    

    I don't know the implications of setting CheckForIllegalCrossThreadCalls to false though.