2010-08-02 40 views
2

在C#.NET GUI应用程序中。我还需要在后台进行一些任务的控制台。基本上,我使用第三方库进行一些处理(花费很多时间),将其中间结果写入控制台。这个处理是一个计算耗时的任务。所以,我将这个任务分配给背景工作者。我的意思是后台工作者称这些库函数。但问题是我没有办法显示计算的用户状态,因为我没有库的来源。我希望控制台能够显示。但令人惊讶的是Console.WriteLine似乎没有工作。我的意思是,没有显示任何控制台窗口。怎么来的?.NET GUI应用程序中的Console.Write

编辑:

我试图设置应用程序类型=控制台。但似乎有一个问题。只有主线程才能访问控制台。控制台上仅显示主(应用)线程执行的Console.WriteLine。由GUI的其他(BackgroundWorker)线程执行,输出不显示。我只需要控制台为后台工作人员。我的意思是,当后台工作人员启动时,控制台启动&时,它结束控制台将关闭。

+0

这与你想要的完全相反:如何隐藏控制台窗口:http://stackoverflow.com/questions/2160338/how-to-hide-console-after-creating-form-in-console-应用 – claws 2010-08-02 04:58:44

回答

2

创建您自己的控制台窗口并使用Console.SetOut(myTextWriter);读取写入控制台的任何内容的方法。

+0

那么,如果我不是写控制台的人,我该如何使用它?我说,第三方应用程序正在写入控制台,我没有它的来源。 – pecker 2010-08-02 05:11:33

+0

如果第三方库是一个.NET程序集并使用'Console.Out',这将使您可以抓取正在打印的任何内容并将其显示在您自己的窗口中。如果它是你调用的本地DLL,'Console.SetOut'将不会有效。 – 2010-08-02 05:14:05

1

将您的应用程序类型设置为“控制台应用程序”。控制台应用程序也可以创建没有问题的GUI窗口,并同时写入控制台。

如果您不具备主应用程序的控制权,并且想要确保显示控制台,则可以输入/调用AllocConsolesignature here)。

虽然这与控制台应用程序不同,但您的应用程序将始终获得单独的控制台窗口,这对于从命令提示符窗口启动它的人来说可能会令人惊讶。您可以通过AttachConsole(s ignature and example here)解决该问题,但输出的shell重定向仍然不起作用。这就是为什么我建议将应用子系统设置为控制台,如果可以的话。

+1

这是令人惊讶的!现在,这引出了一个问题:“控制台和GUI应用程序有何不同?”。到目前为止,我认为GUI应用程序类从'Form'继承,而Console应用程序则不是。还有什么更多? – pecker 2010-08-02 05:10:12

+0

GUI应用程序类不从'Form'继承。在一个GUI应用程序中,你的主函数创建一个派生自“Form”的类的实例,并调用Application.Run(对于WinForms,WPF的一个不同但相似的函数)。您也可以从控制台程序调用'Application.Run',它将在控制台窗口旁显示一个GUI。 – 2010-08-02 05:15:50

+0

下面是一个解释,它也表明GUI程序可以作为一个控制台程序在完成构建之后被标注为:http://www.codeproject.com/KB/cpp/EditBin.aspx – 2010-08-02 05:17:29

0

接下来是@jgauffin,这里是执行Console.SetOut的方法。

创建一个TextWriter继承类。

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; } 
     } 
    } 
} 

并在窗体中,代码如下。

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's 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"); 
     } 
    } 
} 

原始代码是从https://saezndaree.wordpress.com/2009/03/29/how-to-redirect-the-consoles-output-to-a-textbox-in-c/ 您可以查看链接,在评论跨线程调用和先进的实现。