将控制台应用程序转换为使用BackgroundWorker的GUI [英] Converting a Console Application into a GUI that uses a BackgroundWorker
问题描述
我将控制台应用程序转换为Windows窗体应用程序和DLL。 Windows Forms应用程序使用BackgroundWorker来让DLL执行计算繁重的任务。
I am converting a console application into a Windows Forms Application and a DLL. The Windows Forms Application uses a BackgroundWorker to have the DLL perform a computationally heavy task.
在控制台应用程序中,DLL仍然有 Console.WriteLine()
语句。我想将以前打印到控制台的内容指向Windows窗体中的 TextBox
。理想情况下,我想在表单初始化期间从一个流挂钩到一个文本框,并完成它。我担心这可能不是线程安全的BackgroundWorker。
Left over from the console application, the DLL is still littered with Console.WriteLine()
statements. I would like to direct what used to be printed to the console into a TextBox
in the Windows Form. Ideally, I would like to hook a stream from the DLL up to the text box during form initialization and be done with it. I am concerned that this might not be thread safe with the BackgroundWorker.
我发现的最好的方法是更改 Console.WriteLine / code>到
Trace.WriteLine()
,然后按照跟踪侦听器写入文本框(WPF应用程序),但我仍然对BackgroundWorker感到担忧,热衷于将文本附加到现有字符串(有很多文本和IMO字符串并不意味/优化为有大量文本串接在它们上)。
The best approach I have found is to change Console.WriteLine()
to Trace.WriteLine()
in the DLL and then follow the approach at Trace listener to write to a text box (WPF application) but I still have concerns with the BackgroundWorker and am not too keen on appending text to an existing string (there is a lot of text and IMO strings aren't meant/optimized to have lots of text concatenated on them).
推荐答案
以下博客帖子可能是您要查找的:
The following blog post may be what you're looking for:
http://saezndaree.wordpress.com/2009/03/29/how-to-redirect-the-consoles -output-to-a-textbox-in-c /
我想它确切地描述了你要的。基本上,它使用 Console.SetOut
将控制台输出重定向到您自己的流编写器。请阅读帖子下面的注释,了解有关处理主题问题的其他信息。
I think it describes exactly what you're asking for. Basically it uses Console.SetOut
to redirect the console output to a stream writer of your own. Read the comments below the post for some additional information on handling thread issues.
这篇关于将控制台应用程序转换为使用BackgroundWorker的GUI的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!