Convert a console application to a GUI that uses BackgroundWorker

I am converting a console application into a Windows Forms application and a DLL. A Windows Forms application uses BackgroundWorker to make a DLL perform a complex computing task.

Remaining from the console application, the DLL is still dotted with Console.WriteLine() statements. I would like to direct what was previously printed on the console to a TextBox in Windows Form. Ideally, I would like to connect the stream from the DLL to the text field during the initialization of the form and execute with it. I am worried that this may not be thread safe with BackgroundWorker.

The best approach I found is to change Console.WriteLine() to Trace.WriteLine() in the DLL, and then follow the approach in the trace listener to write to the text field (WPF application) , but I still have problems with BackgroundWorker and I'm not too keen on adding text to an existing line (there is a lot of text, but IMO lines are not designed / optimized to have a lot of text concatenated on them).

What is the best way to print old console output into a text box that will be safe with BackgroundWorker?

0
source share
1 answer

The following blog post may be what you are looking for:

http://saezndaree.wordpress.com/2009/03/29/how-to-redirect-the-consoles-output-to-a-textbox-in-c/

I think it describes exactly what you are asking for. It mainly uses Console.SetOut to redirect console output to its own thread maker. Read the comments below the post for more information on threading issues.

+2
source

Source: https://habr.com/ru/post/1435673/


All Articles