1

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


}
}

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)

Why doesn't

CONSOLE.WRITELINE

and similar code work?

1 Answer 1

3

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.

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();
}
Sign up to request clarification or add additional context in comments.

4 Comments

how to do inputs then because when i try to input with Console.ReadLine(); it throws same exception
If this works for Writes, then for reads you need to duplicate the code and instead of StreamWriter, use StreamReader
Thx will put it in my code as soon as i get back on pc
And sorry to see that the answer is not accepted yet.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.