2017-03-05 135 views
0

我有一個程序WFA,它也有命令窗口。我用AllocConsole()打開窗口;當我關閉控制檯窗口時,我使用FreeConsole();但是當我用AllocConsole()再次打開它時;我想寫它並從它讀取,它會引發異常。 代碼:在窗體應用程序中使用控制檯窗口時出現異常

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using System.Windows.Forms; 
using System.Management; 
using System.Management.Instrumentation; 
using System.Collections.Specialized; 
using System.Threading; 
using System.Runtime.InteropServices; 


    namespace WindowsFormsApplication2 
{ 

class classx 
{ 

    [DllImport("kernel32.dll")] 
    public static extern Int32 AllocConsole(); 
    [DllImport("kernel32.dll")] 
    public static extern bool FreeConsole(); 
    [DllImport("kernel32")] 
    public static extern bool AttachConsole(); 
    [DllImport("kernel32")] 
    public static extern bool GetConsoleWindow(); 
    public static bool z = false; 
    [DllImport("kernel32")] 
    public static extern bool SetConsoleCtrlHandler(HandlerRoutine HandlerRoutine, bool Add); 
    public delegate bool HandlerRoutine(uint dwControlType); 
} 



public partial class Form1 : Form 
{ 
    NotifyIcon icontask; 
    Icon iconone_active; 
    Icon iconone_inactive; 
    /*Icon icontwo; 
    Icon iconthree; 
    Icon iconfour; 
    Icon iconfive;*/ 
    Thread Threadworkermy; 

    public Form1() 
    { 
     InitializeComponent(); 
     this.WindowState = FormWindowState.Minimized; 
     this.ShowInTaskbar = false; 
     iconone_active = new Icon(".../iconone_active.ico"); 
     iconone_inactive = new Icon(".../iconone_inactive.ico"); 
     icontask = new NotifyIcon(); 
     icontask.Icon = iconone_active; 
     icontask.Visible = true; 
     Threadworkermy = new Thread(new ThreadStart(checkActivityThread)); 
     Threadworkermy.Start(); 

     MenuItem Nameapp = new MenuItem("xr"); 
     MenuItem quitappitem = new MenuItem("quit program"); 
     MenuItem OpenGUI = new MenuItem("Open GUI"); 
     MenuItem Advancedmodewindow = new MenuItem("x"); 
     ContextMenu contextmenu = new ContextMenu(); 

     quitappitem.Click += quitappitem_click; 
     OpenGUI.Click += OpenGUI_click; 
     Advancedmodewindow.Click += Advancedmodewindow_click; 
     contextmenu.MenuItems.Add(Nameapp); 
     contextmenu.MenuItems[0].Enabled = false; 
     contextmenu.MenuItems.Add("-"); 
     contextmenu.MenuItems.Add(OpenGUI); 
     contextmenu.MenuItems.Add(Advancedmodewindow); 
     contextmenu.MenuItems.Add("-"); 
     contextmenu.MenuItems.Add(quitappitem); 
     icontask.ContextMenu = contextmenu; 

     icontask.Icon = iconone_active; 
     icontask.Visible = true; 
    } 

    private void Advancedmodewindow_click(object sender, EventArgs e) 
    { 
     classx.AllocConsole(); 
     Console.WriteLine("X"); 
     classx.FreeConsole(); 
    } 

    private void OpenGUI_click(object sender, EventArgs e) 
    { 
     this.ShowInTaskbar = true; 
     this.WindowState = FormWindowState.Normal; 
    } 

    private void quitappitem_click(object sender, EventArgs e) 
    { 
     Threadworkermy.Abort(); 
     icontask.Dispose(); 
     this.Close(); 
    } 

    public void checkActivityThread() 
    { 
     try 
     { 
      while(true) 
      { 
       Thread.Sleep(100); 
      } 
     } catch(ThreadAbortException tbe) 
     { 

     } 

    } 

    private void button1_Click(object sender, EventArgs e) 
    { 
     this.WindowState = FormWindowState.Minimized; 
     this.ShowInTaskbar = false; 
    } 


} 
} 

例外,它拋出「System.IO.IOException」在mscorlib.dll其他信息:句柄無效。 對於那些會說改變類型的人,我不能。 (它需要WFA應用程序)

爲什麼不

CONSOLE.WRITELINE 

和類似代碼的工作?

回答

1

異常不是來自打開/關閉控制檯。當你想用控制檯寫/讀某些內容時就會出現。您必須RESET您的控制檯的標準IO。每次必須在AllocConsole()後執行此操作。請檢查下面的代碼,它已經過測試。

CODE:

private void Advancedmodewindow_click(object sender, EventArgs e) 
{ 
    classx.AllocConsole(); 

    TextWriter writer = new StreamWriter(Console.OpenStandardOutput()) { AutoFlush = true }; 
    Console.SetOut(writer); 

    Console.WriteLine("X"); 

    Console.SetIn(new StreamReader(Console.OpenStandardInput())); 
    string line = Console.ReadLine(); 

    classx.FreeConsole(); 
} 
+0

怎麼做輸入,則因爲當我嘗試輸入與到Console.ReadLine();它會拋出相同的異常 – dkman

+1

如果這適用於Writes,那麼對於讀取,您需要複製代碼而不是StreamWriter,請使用StreamReader – Armin

+0

Thx將在我回到PC時立即將它放入我的代碼中 – dkman

相關問題