Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Control-Break in C# console application

Control-Break in C# console application

Scheduled Pinned Locked Moved C#
csharphelpquestiondiscussion
1 Posts 1 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    mSh1985
    wrote on last edited by
    #1

    Hello, I've written a small console application for me and some collegues to use, it contains a lot of commands, all of which we use on a daily basis. To prevent also having to open a windows commandprompt besides my own console, i also included the ping command in my console (started with a process.start) and the results are written in my console. All good and well, but when i keep pinging (ping hostname /t) i have to be able to use ctrl-c and ctrl-break without my console exiting. So i implemented the following in my code:

    static void Console_CancelKeyPress(object sender, ConsoleCancelEventArgs e)
    {
    if (e.SpecialKey == ConsoleSpecialKey.ControlC)
    {
    e.Cancel = true;
    }
    else
    {
    //e.Cancel = true; throws an error
    dowhile();
    }
    }

    the function dowhile() calls a function where i read the input as long as the user doesnt type "exit". I couldnt cancel the event when ctrl-break is pressed, so i guessed i should just call my dowhile function again. Everything works fine, except when i try to close my application typing exit, or pressing the close button. It takes 3 to 5 seconds before my console closes. This only happens when i pressed Ctrl-Break before. Am i doing something wrong, or is there a better way to prevent Ctrl-Break from exiting my console? My console just cant close when i press Ctrl-Break because then i can't see my temporary ping statistics anymore. Any thoughts? Thanks in advance.

    1 Reply Last reply
    0
    Reply
    • Reply as topic
    Log in to reply
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes


    • Login

    • Don't have an account? Register

    • Login or register to search.
    • First post
      Last post
    0
    • Categories
    • Recent
    • Tags
    • Popular
    • World
    • Users
    • Groups