Console in windows Forms application

Asked

Viewed 799 times

4

I have a class where opens the console, shows some data and then closes, when running a second time (without closing the program), an exception occurs in Console.WriteLine("")

  • If executed once, everything works
  • If you close and open the program again, everything works
  • If you try to perform the routine ExportarArquivos() twice, without closing the application before the following error happens:

Error: Invalid transponder.

StackTrace:em System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
 em System.IO.__ConsoleStream.Write(Byte[] buffer, Int32 offset, Int32 count)
em System.IO.StreamWriter.Flush(Boolean flushStream, Boolean flushEncoder)
em System.IO.StreamWriter.Write(Char[] buffer, Int32 index, Int32 count)
em System.IO.TextWriter.WriteLine(String value)
em System.IO.TextWriter.SyncTextWriter.WriteLine(String value)
em System.Console.WriteLine(String value)`

Class:

public static class Exportar
{
    [DllImport("kernel32.dll")]
    static extern bool AllocConsole();

    [DllImport("kernel32.dll")]
    static extern bool FreeConsole();


    public static void ExportarArquivos()
    {
        bool aux = AllocConsole();
        Console.WriteLine(""); //ocorre a exceção aqui (na segunda vez), e aux == true neste ponto.
        Console.WriteLine("Gerando arquivos...");

        //Trabalha um pouco

        Console.WriteLine("");
        Console.WriteLine("Pressione qualquer tecla para sair");

        Console.ReadKey();
        FreeConsole();

    }

}   

Does anyone know what could be causing this?

1 answer

4


It must be because you are declaring twice in a row a variable with the AllocConsole(), create a condition to prevent this. First, declare a boolean checker (as a global variable, not within the method):

private bool isConsoleVisible = false;

and then handle this condition:

public static void ExportarArquivos()
{
    if (isConsoleVisible == false) {
         AllocConsole(); // não precisa criar uma variável para isso
         isConsoleVisible = true;
    } else {
         //FreeConsole(); // se quiser que ele se oculte, "descomente" isso
         //isConsoleVisible = false; // e isso também
    }

    Console.WriteLine(""); //ocorre a exceção aqui (na segunda vez), e aux == true neste ponto.
    Console.WriteLine("Gerando arquivos...");

    //Trabalha um pouco

    Console.WriteLine("");
    Console.WriteLine("Pressione qualquer tecla para sair");

    Console.ReadKey();

    if (isConsoleVisible) FreeConsole; // se estiver visível, desapareça 

}

[EDIT]

It worked like this, with a hint drawn from here.

[DllImport("user32.dll")]
static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);

const int SW_HIDE = 0;
const int SW_SHOW = 5;

public static void ExportarArquivos()
{
    if (isConsoleVisible == false)
    {
        AllocConsole(); 
        isConsoleVisible = true;
    }
    else
    {
        Console.Clear();
        ShowWindow(GetConsoleWindow(), SW_SHOW);
    }

    //Trabalha um pouco

    Console.WriteLine("");
    Console.WriteLine("Pressione qualquer tecla para sair");

    Console.ReadKey();

    ShowWindow(GetConsoleWindow(), SW_HIDE);

}
  • 1

    I did not test this, I thought that as I "Liberated" with Freeconsole, there would be no problem in calling the "Allocconsole", I will test tomorrow, if it works milestone as certain, but it will already take +1. Thank you

Browser other questions tagged

You are not signed in. Login or sign up in order to post.