2010-08-02 44 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/ 您可以查看鏈接,在評論跨線程調用和先進的實現。