Console output in WindowsForms application - c#

I am trying to develope a windows application to display console window as output.
If i changed output type in properties to "console Application" both the console window and form showing. But i need to show only form window first, when i click a button then only the console window need to display the output value.
Please guide me...
Thanks in advance.

Each process can have only at most one console associated with it. There are several paths you can take: either you hide the console window immediately on startup and show it later (via ShowWindow), or you leave the project as a window application, and create the console manually using AllocConsole later.
In the case you wanted multiple consoles, you can e.g. create a dummy process (cmd) and attach your process to its console, using AttachConsole. The managed way would be to use remoting or other techniques to communicate with the dummy process (with your own implementation), and print texts through that.

Use NativeMethods.AllocConsole(); to allocate a console and I/O with it.
Use NativeMethods.FreeConsole(); to close and free this console.

You wanna output the Console.WriteLines of a Windows Forms app inside the form?
In that case just use Console.SetOut(Stream) and pass in something you're wrapping so you can also output it somewhere else.
Another option is to switch all your Console.WriteLines to Trace.WriteLine and add a TraceListener somewhere that does what you want.

Related

How to display a console window in C# Unit Test?

I want to be able to display a console window at will in my C# unit tests that I can later address in my code to print statements to. is there a system command to support for this? My intention is debug purposes but I don't want to use log4net or some other text/xml file for accomplishing that.
Just use Console.WriteLine. Every unit test runner that I've used has captured console output automatically - you don't need an actual console window for that.
If i understand your question correctly, follow the steps mentioned below
1- Right click the console project
2- Go to the Application section
3- In the dropdown named "Output Type" select "Console Application".
4- Save and run the application
what this will do is, it will open the console along with the winform. Whereever you will write Console.Writeline, it will show in the console. (Your form will keep on running as it is.)
If you really need a console window, consider access AllocConsole via p/Invoke:
http://pinvoke.net/default.aspx/kernel32/AllocConsole.html

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");

C# output on both form textbox and console application using RedirectStandardOutput and Error streams

The question here is if anyone has an idea how to both get StandardOutput data to print in Windows form application and keep the output in the console itself. The WinForms part I have completed already, it works great with process events and invoking form elements. At this point I am pretty sure that after re-directing output, Console.Writeline() doesn't print anything on the console window anymore. Any ideas besides running another dummy process that simply displays anything whats on InputStream?
Also
While I'm at this. Is there any way to access process that is on another thread? Since whenever I try to access it it shows that its out of scope.

c#- what steps do i have to go through to convert winform app into console

i have a very simple winforms application.
i do not use the forms object at all. i would like to know what i need to delete or change in order to convert this app to a console. i mainly just need to get rid of all the unneeded references and such
please let me know what steps i should take to do this?
It really depends on how did you code it at first and what pattern are you using. If you have made clear distinctions of what is the back-end and what's not, then you would only have to create a new class to act as the program's Main and change the project's Application Typeand the Startup Object in the project's properties.
If you have all the back-end code intertwined with the winforms then you first need to separate them and then proceed to with the above steps.
You can change the project settings to create a Console Application instead of a Windows Application (just hit Ctrl+Enter on the project in the Solution Window, and make that change).
That will compile the application as a Console Application, and you'll get a console. However, you'll still get your forms. If you want to not have Windows Forms, you'll need to remove the forms, and replace their functionality with command line arguments or configuration. You can then remove the Windows Forms references, and it'll be converted.
Create console app and move the simple code over

What exactly is a "Console"?

I am trying to writing a console application. It has its original console, let's name it console A. And I want this application to do the following things via C#:
Open another console B in another thread, then get input from A and output it to B;
type a command in A, such as dir, and show the output in B;
while doing the above things (still not done yet. X_X ), I find myself lack a through understanding of what a console window is, and how it is assigned to a console application, especially the very first console when my console application starts to run. Could some one shed some light on me?
Is console window physically a memory area in the video memory? Or something else?
Could different threads within the same process have different console of its own for its own I/O?
Many thanks.
Now I am using one console application to start another console application in a new process. Thus I can have 2 consoles output at the same time.
My understanding now is that, for Windows OS, a console is a special window, and it's a system resource that OS assigned to the application without-a-UI as a necessary user interface. Windows OS handles the wiring between the system-prepared console window with our UI-less application.
In Windows terms, a Console is a textual GUI window that you see when you run "cmd.exe". It allows you to write text to, and read text from, a window without the window having any other UI chrome such as toolbars, menus, tabs, etc,..
To get started you'll want to load Visual Studio, create a new project and choose "Console Application". Change the boilerplate code that Visual Studio produces to:
using System;
using System.Text;
namespace MyConsoleApp
{
class Program
{
static void Main(string[] args)
{
Console.Write("Hello, world!");
Console.ReadKey();
}
}
}
When you run your application, a console window will open with the text "Hello, world!" and it'll stay open until you press a key. That is a console application.
Is console window physically a memory area in the video memory? Or something else?
It's not physically a memory area in video memory, it's "something else". The Wikipedia Win32 console page gives a fairly robust descrption of the ins and outs.
A console application has only one window. It does not have window management functions in order to spawn child "consoles".
You can start additional console applications, but these are separate entities.
No. It's a windows GUI subsystem. In WinAPI there are functions to work with console: http://msdn.microsoft.com/en-us/library/ms682073%28VS.85%29.aspx
A (OS) console is a process (containg one or more threads of executions, all of them sharing the same memory space), and this process has:
standard input (a stream of input bytes), what you key in
standard output (a stream of output bytes), what the program prints
standard error (a stream of output bytes), what the program prints when it's complaining about something
So if you want to create another console (from .Net) and link the input/outputs I understand you have to create a process (executing "cmd.exe" by example).
I don't know the API of .Net for process manipulation but if it's like Java you can hook up stdin, out and err so you can play with your created process from the original one.
A windows application can have one console, or no console, it can't have more than one. See the documentation for AllocConsole.
The console is basically an emulation of the 'pre-Windows' days when there would literally be a 'control console' i.e. a keyboard and screen attached to a mainframe computer.
To do what you want you could spawn another process with its own console and communicate between the two, or make a GUI application which looks like a console window.

Categories