Key Pressed Event / Ending thread

This is a discussion on Key Pressed Event / Ending thread within the C# Programming forums, part of the General Programming Boards category; In my server program I have a thread set up to listen for requests, and assigns each incoming request to ...

  1. #1
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913

    Key Pressed Event / Ending thread

    In my server program I have a thread set up to listen for requests, and assigns each incoming request to another thread. I'd like to set up an event handler that fires when a key is pressed to execute a number of different options depending on which key is pressed. I have two questions:

    1) What event handler should I use? I don't have a C# book - I'm basically learning from some tutorials and MSDN's reference section, so my knowledge in this area is impaired somewhat. If I could just get the name of what would be the ideal handler, I can look it up on MSDN and figure it out from there.

    2) I've started the threads and sockets, but I don't know how to end them. Do I just use the delete keyword on those objects, or what? I'd like to do this from the event handler, to pass control from the threads, back to a traditional console application style.

  2. #2
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,138
    If you start a thread by using a System.Thread object's Start method, you can terminate the thread by using it's Abort method. Your thread method will then throw a special exception you can catch in your thread or just not catch it if you want the thread to be terminated.
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

  3. #3
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    Cool. Thanks. I've been paging through MSDN looking at event handlers. Do I want to use something like KeyDown or is there a better one to use?

  4. #4
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    Also - once I've done Thread.Abort(), can I reuse the thread by invoking Thread.Start().Again. Then, once I'm donce completely with the thread, do I need to use the delete operator on it?

    And what about with sockets? Just delete them?

  5. #5
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,138
    All managed objects ( if you don't know if it's managed, then it is ) will be deleted by the garbage collector once the last reference to the object goes out of scope. Actually, it's not instantenous, the GC will run once in a while and decide to destroy the object when it sees fit.
    Anyway, the safe way would be to .Close() the socket and set the Socket instance to null. Same with the Thread, .Abort() it and set the Thread instance to null.
    If one of those implements IDisposable, you can call .Dispose() to call some kind of destructor that frees resources at a set time and does not wait for the garbage collector to have a good day and clean up.

    I don't know if you can .Start() an aborted Thread again... maybe it's safer to create a new Thread.
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

  6. #6
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    Alright. Thanks. The idea here is that the server is running and "serving", and then when the user hits a key, one of the options is to diconnect. At that point I'd interrupt the thread and socket and return to a regular consol app. They could then change a few options, and possibly start the server again, so I think I'll go with the method of creating a new thread.

    edit: And I just realized I still don't know what event handler to use - is there an event handler that listens for any keyboard input? Anything close? (Other than Console.Readline() from the main execution thread)
    Last edited by sean; 07-05-2004 at 01:34 PM.

  7. #7
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913

    Problem Solved!

    Well nvoight - thanks a lot for all your help - I think I've finally gotten this all straightened out.

    What about the event handler you ask? (Yeah... like you're actually asking that...) My brother and I figured out a solution, after determining that there was no such event handler (I'm a little disappointed that .NET did not have this event handler, which seemed a bit simple. I also learned that it was missing a few other seemingly basic components for building console apps. But I managed to make it work. The solution is this:

    This all stemmed from the fact that I didn't fully understand threads. I have my main function, which calls a "serve" thread. This thread would listen for requests and assign each one to a new "client" thread. Instead of having an event handler listen, I merely placed Console.Read() after my main function started the serve thread. That way, the threads start up, and while they executing, the PC is still just waiting for input. It seems so obvious that I'm no kicking myself.

    After the call to Read() (which is basically getch() with some differences - but for the purposes of this discussion...) I have the obvious code for processing what key was pressed, and then depending on the selection, it'll start the process of shutting down all connections and all client threads, and returning to a more traditional console application state.

    Thanks again nvoight!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 11
    Last Post: 05-10-2009, 08:51 AM
  2. Wich key is pressed on the keyboard
    By Coding in forum Windows Programming
    Replies: 0
    Last Post: 03-20-2008, 02:20 PM
  3. About aes
    By gumit in forum C Programming
    Replies: 13
    Last Post: 10-24-2006, 03:42 PM
  4. Virtual keys
    By Arkanos in forum Windows Programming
    Replies: 4
    Last Post: 12-12-2005, 09:00 AM
  5. Registery with api?
    By ismael86 in forum Windows Programming
    Replies: 1
    Last Post: 05-14-2002, 10:28 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21