how to close console window after form is closed - c#

i have a console application project. it has gui (winforms) and console window which is used as debug console (application's run log).
now, if i have got debug disabled i hide console window with ShowWindow native call.
the problem is: after i close application window by pressin the X button or by quitting it (Application.Exit()) i have a zombie console process.
if i close console on the very begining by Environment.Exit(0) the form is not been showed.
i have tried to modify forms Closed event by adding there Environment.Exit(0), but that did not work which means that there still remains a zombie console process.
how can i close console window on application exit?

If it is a console application, the console will not "close" until the process terminates. So you just simply need to close the form normally... assuming you "opened" it like you should.
To "open" the form, you need to run it. Run it by calling Application.Run(Form) (the same way you would initialize an actual form project). Since a console is allocated to your process, your form may write to Console.Out and you should see what you write.

Related

How to create a console application that runs in background?

I created app that open every time that I start pc. So its so annoying to close it every time so I'm wondering if its some code that will hide my console app. I saw videos and tutorials on forms but idk how to do it with console app.
The easiest way to do this is change your console app to a windows app.
Console apps get a console made for them by Windows. But if you change it to a windows forms app, then windows expect the application to make a window, so if you never make a window, then it will never show
The other way is to turn your application into a service. This has some additional requirements in terms of programming
Option 1
You can use this run command:
start /min "SomeTitle" MyConsoleApp.exe myarg1 myarg2
Thus it will be on the taskbar minimized.
Option 2
If you use a file link in the start menu, select the start minimized option for the exe.
Option 3
Using a WinForm app you will be able to use a tray icon by setting the main form as not visible, to say it simply because it can be complex according to the expected behavior, and it will not be in the taskbar too.
Option 4
If you don't want a main form, create a win form app, delete the form file and the code in the main method, and you're done, without GUI nor console, no main input and no main output but you can show MessageBox and some forms when necessary, just a background process only visible in the Task Manager.
With that you can add a tray icon to to offer exit and some status information for example.
Option 5
Also you can create a windows service:
.NET console application as Windows service
Note
In all cases, if you don't use an internal message events dispatcher like the WinForms Application pattern or WPF and so on, be carefull to not saturate the CPU with the processings like with loops and use Thread.Sleep() between iterations or any thread idleing pattern or some timer if necessary.

How to distinguish between a program’s GUI and Console window?

I have written a code in c# that gets processes by name, activates the window of the desired process and sends some hotkeys to this process. The problem is that the desired process has two windows, a console window and a GUI window, as a result sometimes the GUI window is activated and everything works fine, but sometimes the console window is activated and nothing happens. So my question is, is there a way to distinguish between that process’ GUI and console window, so by program won’t get confused and only activate the GUI window ?
(By activating I mean bringing this process’ window to the foreground in order for the sending hotkeys to work. I have no access to the code of the desired process so I cannot change the way it starts up).

How does x_FormClosing work?

I am developing Windows Form application and I wanted to execute some code when the user closes the window. Most people did it by using
This didn't work, and I've seen that you have to add events like this
Then, it actually worked, the code executes when I close the window but the next problem was there. The application didn't exit at closing the window. So I tried to add "Application.Exit();"
Now, it seemed to work. But it prints out the debugging message "Closed" two times. So it seems like even if the form is closed, Application.Exit() triggers the x_FormClosing method a second time ... any ideas?
The Form hasn't fully closed yet and Application.Exit is going to call .Close on all open forms. Closing allows for the operation to be cancelled.
An application, by default will exit after all forms are closed. If you want this form to be a "master", consider putting it in the Closed event.

C# Console Application Keypress capturing

Is it possible to check pressed keys in a Console Application in any other way than Console.ReadKey()?
I've changed the project settings to Windows Application so that the console window won't show. But if the console window isn't shown the application crashes since Console.ReadKey() requires a console window. Is there any workaround?
NOTE:
I don't want any window open, just track a key-press, through a thread.
You cannot receive key pressed event if you do not have a window. You hae to have a console or "desktop" window.
In case of console 'Console.ReadKey()' does the job, in case of WPF or Windows Forms 'KeyPressed' event does the job.

Console and Windows Forms in C#

Ok I'm creating an application with a plugin architecture and the application would be able to run without a GUI in other words the GUI is really optional... and if the user decides to use the GUI the console is just hidden.
I can create the form in the console by calling one of the plugins method, but as soon the Window is created the console program keeps waiting until the window is closed... is there a way to create the form so that the console doesn't have to wait, it should keep working on it's own stuff and just notify the gui with some info?
Why don't you keep the windows forms application as a seperate executable and call process.start() ?
For example
Process.Start("c:\yourwindowsformsapplication.exe");
You can quit your console application or continue other work within the console application once you start the windowsapplication.exe.
And use remoting to communicate between both the applications.
or....
Create a new thread and call your form.show() within a new thread
Example :
Form frm=new form();
Thread th=new Thread(frm.show);
th.start();
Are you creating and then showing the form using ShowDialog()? If so, that's why the console app is waiting for the form to close. Try showing the form using Show() instead - Show() is a non-blocking call that will return program execution to the next line.

Categories

Resources