Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run();
var sim = new SimMain();
SimInterpreter interpreter;
interpreter.interpreterMain(sim);
Application.Exit();
I'm trying to make a console centric application, with the option to popup a form when a specific command in entered. But I can't figure out a way to get past the Run() statement, as the app freezes at that point. I need help!
I tried using Run(new SimMain()), but it ended up putting more emphasis on the form than the console, and I couldn't find a way to hide it. Running the console handover code before Run() worked to give the console control of the app process, but the components from the form didn't display when the command for the popup was entered.
I googled multiple keywords, but articles relating to formless applications are scarcer than water in the desert.
You need to understand how console vs UI programs work.
In a console program you have a 'main' thread used for running things. You always have control of this thread. When this thread exits the main-method your console program terminates.
In a UI program you have a 'UI thread'. You do not have direct control of this thread in the same way as a console program. It will instead run a "message pump/loop" that processes events from the OS. This is essentially a infinite loop that processes messages from a queue. So if you click a button there will be a "mouse down" message sent from the OS. The UI-code will figure out what button you clicked, and raise the corresponding button event handler. From there you can do whatever it is you need to do. But you will need to return control back to the message pump. Otherwise the "application has hanged" message will show.
Calling Application.Run()
will start such a message pump, without actually showing any form. So "freezing the application" is the expected behavior.
If you need the console-part to work at the same time as the UI-part I would create a new thread, and show your form on this thread, using ShowDialog()
. This will also start the message pump, and make this new thread the "UI thread". But you need to be really careful to make this work correctly.
I would only recommend this if you are really comfortable with multi threading and thread safety. Otherwise I would recommend either making a UI or a console application, or perhaps decide at startup what it is going to be. There might also be other solutions depending on the details of your specific use case.