Advertisement

message handlers in console apps?

Started by May 10, 2002 02:18 PM
4 comments, last by wild_pointer 22 years, 9 months ago
I have a small console utility that runs continuously. I was wondering is there a way I could give it input without interupting its execution? Is there a way to implement some kind of message handler, is this a job for multithreading? [Gadzooks|SGI STL|Bjarne FAQ|C++ FAQ Lite|MSDN|Jargon] [edited by - wild_pointer on May 10, 2002 3:19:26 PM]
[size=2]
One way would be to fill out a window class, create the window - just don't show it - use a WndProc and GetMessage loop.

// EDIT: Thinking about this a little more - you could make it a dialog app, dump the icon to the tray to open the dialog to make adjustments etc. That is unless you want the console window to show all the time. What exactly are you trying to do?

[edited by - lessbread on May 10, 2002 3:39:27 PM]
"I thought what I'd do was, I'd pretend I was one of those deaf-mutes." - the Laughing Man
Advertisement
The console currently just displays debug info for the AIM bot I wrote. (Not a very respectable project, but hey it helped me learn how window handles work ) I was hoping to be able to change some settings through the console without having to restart the program.

It actually originally started as a windows dialog app, but I quickly learned I didn't have the skill and whent to a console app. But if that's the only way I guess it's as good as any reason to learn.

Thanks

[Gadzooks|SGI STL|Bjarne FAQ|C++ FAQ Lite|MSDN|Jargon]
Ripped off from various people

[edited by - wild_pointer on May 10, 2002 4:09:31 PM]
[size=2]
Creating a new thread might be the most appropriate solution (instead of having a window that is never used):

  // assume all appropriate headers includedint main( void ){  DWORD ThreadId = 0;  HANDLE hThread = NULL;  hThread = CreateThread( NULL, 0, ThreadProc, NULL, 0, &ThreadId );  if( !hThread )  {    cerr << "Failed to create worker thread. Aborting..." << endl;    return -1  }  // main loop processing  while( true )  {    // yadda  }  return 0;}//DWORD WINAPI ThreadProc( void * pArg ){  // create message queue  MSG msg;  PeekMessage( &msg, 0, 0, 0, PM_NOREMOVE );  // message pump/processing  while( true )  {    if( !PeekMessage( &msg, 0, 0, 0, PM_REMOVE ) )      WaitMessage();    switch( msg.message )    {    case TM_MYMSG:      // do processing      break;    case TM_EXIT:      return 0;    }  }  return 0;}  

Thanks Oluseyi, Anyone know of a good tutorial on multithreading in C++? I found one on flipcode, but they kinda sucked, and there don't seem to be any here. I'd like to know what im doing instead of just copying your code.



[Gadzooks|SGI STL|Bjarne FAQ|C++ FAQ Lite|MSDN|Jargon]
Ripped off from various people

[edited by - wild_pointer on May 11, 2002 3:42:51 PM]
[size=2]
quote:
Original post by wild_pointer
Anyone know of a good tutorial on multithreading in C++?


Take a look at the MSDN documentation for CreateThread, __beginthread and __endthread. Then look at the pages for other thread functions. Under Window, also look at the messaging functions [Get/Wait/Peek/Send]Message, the MSG structure and related topics.

I''m sure there are tutorials out there, but I''m not a fan of tutorials.

This topic is closed to new replies.

Advertisement