Start conhost.exe without console window flashing - c#

I'm writing an embedded powershell host in C# using VS13 (Windows Application NOT console app). I can invoke powershell commands just fine. The issue I'm having is that when the invoke function handles a console command like ping, netstat, etc. it triggers an instance of conhost.exe to be created. This is normal behavior for Windows 7 and above from my research so far, but when conhost starts up, it briefly flashes a console on the screen.
Is there a way to have conhost start-up without flashing the console window initially? Keep in mind that I'm handling user input into the powershell pipeline so testing for each command, and starting a new process is probably not practical.

It's been a while since I asked this question so I wanted to share the answer I came up with. Basically, since my embedded host was in an application that runs solely in the background, there was no way to prevent conhost from flashing on the screen when "native commands" were processed. It's just the way Windows works. If my host was attached to a GUI then I think there might have been a way to redirect output, but so far for my use case I couldn't suppress conhost.

Related

How to launch in system tray icons a MVC app

I'm developing a MVC application using .NET 6.0. I am publishing the app with the following configuration:
The app, so far, is launched by double-clicking the .exe, it shows the classic "cmd-style" window.
Now the requirement is to start the portable app minimized into a system tray notification area in Windows (if this is not possible, I was looking for a method to deploy the app as a service which runs in background).
How can I achieve this? Thank you.
To obtain access to the system tray, you need a message pump and a target window. In other words, a regular command-line executable doesn't cut it. Basically you need to have an executable that creates a window (can be an invisible one) and then the main thread must pump Windows messages.
I'll say this much for now becuase the provided information is insufficient and explaining all possible scenarios would be too long a response.

C# Close any application running in System Tray

I want to properly close any application (not kill) running in system tray. I have tried SendKeys() but it fails. Sending Alt+F4, Alt+F then x all fail because tray applications have no main window. Any idea how to do that? the objective to to properly exit an application that performs some inner tasks upon exiting, running in system tray without terminating, just like when its titlebar exit button is clicked.
There is no simple answer to this. Each and every application has its own way of controlling life time. You can even write Forms application that does not respond to [Alt F4] to shut it down.
The only way you can an arbitrary process of which you know nothing (other than that it has put an icon into the system tray), is to terminate the process. And that, as Gian Paolo pointed out, is just not cricket.
From what you have written on the comments on the other answers, it sounds like you merely want to close a specific app. You can do this with the taskkill utility.

Enabling a console application through a Windows Forms application

I'm not going to go into details why am trying to do this, instead of making the main application do the work. I think it's currently easier for me. But I'm not going to use this technique in the future.
In my case, the main form has a button that opens another form. In the second one for you can adjust the amount, pause, resume and stop the work of the console application (sound totally useless (and maybe stupid), but, like I said, I'm not going to go into details why). This means that the application must have access to all the variables and resources of the whole program and vise versa.
I know how to launch a new form trough a main form, but I don't know how to launch a console application.
EDIT:
I forgot to mention, that the console application is a part of the solution.
Your requirement is a bit vague; "the application must have access to all the variables and resources of the whole program and vise versa". 'Variables and resources' cannot be shared across processes, you will instead need interprocess communication of some form.
If your console app merely needs to communicate back to the calling forms app that a RPC has succeeded then use exit codes in the console app, see: How do I return a value from a console application to a service in .NET?
Otherwise this has been answered before: Getting the ouput from Console window into Winform application
You'll need to either create a console emulator (time consuming and difficult to get right), or fire up cmd.exe in another process and use remote procedure calls (or another inter process communication method) to communicate between the two processes
If you want to communicate between the two processes, take a look at this library here:
https://github.com/TheCodeKing/XDMessaging.Net
It allows you to send messages from one app to the other. For example, App1 sends a message "stop" on the channel "randomkey" to ConsoleApp1, ConsoleApp1 can listen on the channel "randomkey" and intercept the "stop" message and stop its current processing.
If you wanted to just open the console window, just use System.Diagnostics.Process.Start();
You can just call Main directly. Beware of doing this on the UI thread directly though!
SomeConsoleApp.Main(new string[]{"-O", "File 1.txt", "-some-parameter"});
Or if you only have an exe, you can do:
System.Diagnostics.Process.Start("someconsoleapp.exe");

Mono & WInforms on OS X

I have just begun to explore the mono winforms environment and I cannot work out how to start a program from within monodevelop without a console session being started.
My simple program runs okay but when it exits a terminal session is always created & waiting for me to 'press any key'. I guess I could arrange things so that the terminal window closes automatically, but I would rather the app just ran 'natively', is this possible or does the way mono & .net function work preclude it?
As shown in the examples at Zetcode, in 'Main' the rest of the code is started with 'application.run(new aFunction());', I thought this might be the cause of the terminal session occurring but replacing it with:
myNewClass n = new myNewClass();
n.aFunction();
causes the program to not run at all (or maybe just exit without doing anything).
I am an experienced programmer but not familiar at all with C# or the mono/.net environment so 'stating the obvious' may be all that is required in an answer.
MonoDevelop will usually let you start a program with or without it running in an external console.
In MonoDevelop 2.8 on Linux you can control this by context clicking on a project in the solution tree and selecting Options; then Run on external console under the Run section. I'm not sure if you can disable this on OSX.

SendKeys.SendWait doesn't works

My target is to send keyboard events to external application.
From my application, I'm launching a C# exe (console application) that bring the target application to the front and uses SendKeys.SendWait to send keyboards events. I ran into a rate case were the command don't have any affect.
When debugging it, it works but when running it not in debug it fails.
I think it as something to do with the fact that when debugging my application is the active application.
You'll need to do a little work, and it changes depending on the version of Windows. There's an MSDN page that has a good explanation and an example:
http://msdn.microsoft.com/en-us/library/ms171548.aspx

Categories