Console.Write in .NET GUI Application

In a C # .NET GUI application. I also need a console in the background for some tasks. Basically, I use the Thrid Party library for some processing (takes a lot of time), which writes intermediate results to the console. This processing is a computational task of time. So, I assign this task to a background worker. I mean, the background worker calls these library functions. But the problem is that I have no way to show the status of the user of the calculation, because I do not have the source of the library. I was hoping the console would be shown. But surprisingly Console.WriteLine does not seem to work. I mean, the console window is not displayed. Why?

EDIT:

I tried setting the application type = console. But it seems to be a problem. Only the main thread can access the console. Only the Console.WriteLine console, executed by the main (Application) thread, is displayed on the console. Console.WriteLine , executed by other (BackgroundWorker) GUI threads, the output is not displayed. I need a console only for background workers. I mean, when the background worker starts up, the console starts up and when it ends, the console shuts down.

+2
source share
3 answers

Create your own console window and use the Console.SetOut (myTextWriter) method ; to read everything that was written on the console.

+2
source

Set the application type to Console Application. Console applications can also create GUI windows without problems and simultaneously write to the console.

If you do not have control over the main application, and you want to make sure that the console is shown, you can p / invoke AllocConsole ( signature here ).

This is not the same as being a console application, but your application will always have a separate console window, which may be surprising for someone who started it from a command prompt window. You can get around this with the AttachConsole (s ignature and example here ), but redirecting the output shell will still not work. Therefore, I suggest setting up the application console if you can.

+1
source

Followed by @jgauffin, here is the implementation of the Console.SetOut method.

Create an inherited TextWriter class.

 using System; using System.Text; using System.IO; using System.Windows.Forms; namespace ConsoleRedirection { public class TextBoxStreamWriter : TextWriter { TextBox _output = null; public TextBoxStreamWriter(TextBox output) { _output = output; } public override void Write(char value) { base.Write(value); _output.AppendText(value.ToString()); // When character data is written, append it to the text box. } public override Encoding Encoding { get { return System.Text.Encoding.UTF8; } } } } 

And in the form as shown below,

 using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.IO; namespace ConsoleRedirection { public partial class FormConsole : Form { // That our custom TextWriter class TextWriter _writer = null; public FormConsole() { InitializeComponent(); } private void FormConsole_Load(object sender, EventArgs e) { // Instantiate the writer _writer = new TextBoxStreamWriter(txtConsole); // Redirect the out Console stream Console.SetOut(_writer); Console.WriteLine("Now redirecting output to the text box"); } // This is called when the "Say Hello" button is clicked private void txtSayHello_Click(object sender, EventArgs e) { // Writing to the Console now causes the text to be displayed in the text box. Console.WriteLine("Hello world"); } } } 

Source code from https://saezndaree.wordpress.com/2009/03/29/how-to-redirect-the-consoles-output-to-a-textbox-in-c/ You can check the link to calls with cross streams and extended implementations in the comments.

0
source

All Articles