CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    May 1999
    Location
    Saint Paul, Minnesota, US
    Posts
    91

    Using gets() in a thread

    Hi --

    I have a Win32 console application that takes user input using gets(). When the user presses the ESC key I would like to exit the current 'thing' the user is doing.

    gets() seems to 'take over' until the user presses the RETURN key.

    I am attempting to create two threads. The first thread is a UI thread that will do the get() functions and the second thread will monitor keypresses or messages for the ESC key.

    I am having problems with thread priority. I have tryed some different priority settings and either get the user input and not the ESC key - or I get the ESC key and there is not user input.

    Any comments ?
    Thanks,
    Chris


    ///////////////////////////////////////////////
    // Starting the threads in the main()

    // Create the monitor thread
    Threadhandle = CreateThread(NULL, 0, ThreadprocESCMonitor, 0, 0, &Threadid);

    // Set the priority
    SetThreadPriority(Threadhandle, THREAD_PRIORITY_ABOVE_NORMAL);

    // create the user interface thread
    Threadhandle = CreateThread(NULL, 0, ThreadprocUserInterface, 0, 0, &Threadid);

    // Set the priority
    SetThreadPriority(Threadhandle, THREAD_PRIORITY_HIGHEST);


    ///////////////////////////////////////////////
    // Thread ThreadprocUserInterface
    DWORD WINAPI ThreadprocUserInterface(LPVOID lpParameter)
    {
    while (bLoop)
    {
    if (gfkbhit())
    {
    // The gets() are in the HandleCommands Function
    HandleCommand(getkey());
    }
    }
    return 0;
    }



    ///////////////////////////////////////////////
    // Thread ThreadprocESCMonitor
    DWORD WINAPI ThreadprocESCMonitor(LPVOID lpParameter)
    {
    while (bLoop)
    {
    // Test for a key press
    if (gfkbhit())
    {
    if (getkey() == ESC)
    {
    Beep(1000, 10);
    }
    }
    }
    return 0;
    }

  2. #2
    Join Date
    Feb 2002
    Posts
    5,757
    What is "ESC" in this line?

    if (getkey() == ESC)

    VK_ESCAPE

    Kuphryn

  3. #3
    Join Date
    Aug 2001
    Location
    Stockholm, Sweden
    Posts
    1,664
    gets() blocks until return is pressed.

    CreateThread() isn't good to use together with C run-time library functions (like gets(), printf() etc). You should be using _beginthread or _beginthreadex instead of CreateThread (and compile with /MD).

    I don't know how the keyboard buffer queue is handled, but I would maybe create my on input function (similar to gets()) that doesn't block and then skip the ESC key check in the other thread. I think you have made it more complicated than it really is.

  4. #4
    Join Date
    Aug 2002
    Posts
    58
    Or, your one thread can grab _all_ the keystrokes including ESC, and use Create/Reset/SetEvent() and WaitForSingleObject() to signal the other thread that ESC has been pressed.

    That way you don't have the two threads fighting for the input buffer and your second thread doesn't have to spin forever. (Or your first one, either, since you'll now be free to use blocking input methods in that thread.)

    Code:
    HANDLE hESC = CreateEvent(NULL, TRUE, FALSE, NULL);
    
    ...
    
    DWORD WINAPI ThreadprocUserInterface(LPVOID lpParameter)
    {
    ...
      if (getkey() == ESC)
         SetEvent(hESC);
    ...
    }
    
    DWORD WINAPI ThreadprocESCMonitor(LPVOID lpParameter)
    {
    ...
    WaitForSingleObject(hESC, INFINITE);
    // ESC has been pressed!
    ...
    }
    HTH,
    Bassman

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured