If this is possible, I would like to make in C# a wpf application that has a console window in it. Also, I would like to have the console interact with the WPF form so that if you are complete with a console activity it can tell the wpf application and close. I guess this would be a basic inheritance program, but with a console/simulated console included.
You want something called interprocess communication.
There is a lot of ways of do that, but the easiest is the Named Pipes IMHO.
Related
I have a quite small (so far) project which consists of
Core
GUI
TUI
The TUI (console application) is communicating with another console application in order to send and retrieve certain information.
The purpose of the GUI is to make it easier.
However, I am stuck on how would I make my TUI communicate with my GUI. So far, I know I can start my TUI from the GUI like this:
System.Diagnostics.Process.Start(#"cmd.exe", #"/k C:\project\TUI\bin\Debug\TUI.exe");
But now, I do not have any reference to the console application and I do not know to send information forth and back. I tried to search for the answer, but my search would only lead to how to start them in a normal way (not both, together, communicating).
So all in all, my question is: How to start a console application from windows forms project so those two to be able to communicate?
You can use the Input and Outut Streams of the Process.
Read this :
https://msdn.microsoft.com/de-de/library/system.diagnostics.process.standardinput(v=vs.110).aspx
Is there any way I could have an application that acts as a different interface for a powershell prompt, like the ISE does? I just want to make it visually different. I'm thinking something to do with stdin and stdout, but i'm not totally sure how to make that work in C#. As well as the issue of hiding the console window.
Sure you can use the PowerShell engine API to host PowerShell within a WPF or WindowsForms application. See this MSDN topic on hosting PowerShell in your own application.
I have a WPF application, and i need to launch some actions (and receive return values) starting from another console application.
I encountered many problems so i don t know exactly how to proceed:
1- I tried to use command lines with arguments(it worked to launch the wpf application), but i couldn't receive return Values, because they are only returned on application shutdown. Also it doesn't fit my need since some actions must be called while the wpf app is still running.
2- I thought abt developping a small dll to communicate between Console and WPF application, but i don't know what technology would be light, efficient and fit my needs.
Has any one achieved similar task in the past? If so, how did you go about it?
Thanks,
What you are looking for is usually termed 'Inter-process communication'. Named pipes are a type of IPC and can be used in .net. This guide should get you started.
You could use WCF technology to achieve this. Define contract in you wpf application, where you should set your action logic - methods, by which you want to manage your application. If it is single sided, use netTcpBinding and create endpoint in your WPF application - it is a listener from the commands from outeside. Then in your your console application add proxy class of your wpf application, using svcutil. This will allow you to send your commands. If you do everything right - this should work. I suppose you have 1 wpf application and 1 console application.
You may launch your wpf app and then use:
Named Pipes - the simplest way but you must create a protocol of interaction between apps. You may transfer serialized objects for example. (example);
Shared files;
WCF - most powerfull and complex technology.
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");
Is there an easy way to do this?
I am testing my networking application using just the console for now. What would be nice is to have multiple consoles from one project and one press of the "Debug Now" menu item.
I could, like I have in the past, use multiple projects but that's seems unwieldy. Ideally I could launch multiple console instances (running from the same thread is fine) and have them not cover the other consoles when they do launch. Launching side by side would be awesome!
How practical is what I'm asking? Is it possible?
Thanks!
There is no easy way to do this.
Technically, you can create a separate console for an application, but it requires creating a child process to host the console. There is a CodeProject article showing the basic procedure.
That being said, at the point where you want multiple "windows" showing data, I think migrating to a (simple) GUI application is a better choice.
You could build & start a master application that runs and positions your test applications. See what the System.Diagnostics.Process class can do for you.
The real problem however is in debugging multiple instances of the same app at once. I'm not sure that that is possible.
System.Diagnostics.Process.Start("MyOtherProgram.exe");