在表单应用程序中使用控制台窗口时出现异常 [英] Exception when using console window in a form application

查看:22
本文介绍了在表单应用程序中使用控制台窗口时出现异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个程序 WFA,它也有命令窗口.我用 AllocConsole() 打开窗口;当我关闭控制台窗口时,我使用 FreeConsole();但是当我用 AllocConsole() 再次打开它时;我想写入和读取它,它会引发异常.代码:

I have a program WFA that also has and command Window. I open the window with AllocConsole(); When I close the console window, I use FreeConsole(); but when I open it again with AllocConsole(); I wanna write and read from it and it throws an exception. The code:

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


}
}

在 mscorlib.dll 中抛出System.IO.IOException"的异常附加信息:句柄无效.对于那些会说要改变类型的人,我不能.(必须是WFA申请)

Exception that it throws out 'System.IO.IOException' in mscorlib.dll Additional information: The handle is invalid. To those who will be saying to change the type, I can't. (it needs to be WFA application)

为什么不

CONSOLE.WRITELINE

和类似的代码工作?

推荐答案

异常不是来自打开/关闭控制台.当您想使用控制台写入/读取某些内容时,它就会出现.您必须重置控制台的标准 IO.每次必须在 AllocConsole() 之后执行此操作时.请检查下面的代码,它已经过测试.

The exception is not coming from Open/Close the console. It's coming when you want to write/read something with the console. You have to RESET the standard IO for your console. Every time you have to do this after AllocConsole(). Please check below code, it's tested.

代码:

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

这篇关于在表单应用程序中使用控制台窗口时出现异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆