Multiple Executions

How can you make two (or more) functions execute simultaneously?  For example, say the program is asking for input; normally, all other executions would stop right there until the user enters something, but how can I have other functions running at the same time (a timer, for example)?
strider031598Asked:
Who is Participating?
 
TMSConnect With a Mentor Commented:
With DOS, you can poll the keyboard (can't remember any interrupt number, function number or name, I just know it is possible) to check if a key hass been pressed. (I guess INT 16h is the one for keyboard, though I wouldn't swear, too long ago :)
So pseudocode for what you can do is...

While(WHATEVER_CONDITION_CONTUNUES_THE_LOOP)
  {
   Do_Whatever_Funky_You_Have_To_Do();
   Do_Something_Else_But_Not_TooLong();
   Make_Any_Calls_You_Want();
   if (Key_Has_Been_Pressed())
     {
      Key = Read_Key_From_Keyboard();
      Do_Whatever_You_Want_With(Key);
     }
  }

Sorry I can't remember any function names but any good book/compiler documentation should tell you as I showed you where to look.

Hope this helps.
0
 
WxWCommented:
There are two ways to do that :

1st : Use SetTimer() Function to create a system timer
UINT SetTimer(

    HWND hWnd,      // handle of window for timer messages
    UINT nIDEvent,      // timer identifier
    UINT uElapse,      // time-out value
    TIMERPROC lpTimerFunc       // address of timer procedure
   );

2nd : Create another thread for your application , using CreateThread and TerminateThread Functions ( better _beginthread and _endthread for C++ ) .

Now If you want the two functions to be perfectly synchronized , you should check the System Time in each function to check if they run at the same time.
0
 
nietodCommented:
That only works in a windows program.   Is this a windows program?  You can also use multiple threads, but again that only works on OSs that have multiple threads.  There are other posibilities if you don't have windows/ or multiple threads.
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
WxWCommented:
Yes sure I assume that it is a Windows Program

0
 
nietodCommented:
There is a good chance that this is not a windows program.  This tends to not be an issue in a windows program.  Why?  Because you don't "get" input in a windows program.  You create a window to obtain the input and it is fulled in by the user.  Then some event occurs to indicate the input has been entered.  This sort of problem tends to occur only in console applications, because they must ask for input and wait for it to be entered.  (Well not MUST but that is the easiest.)
0
 
alexoCommented:
Several Unixes support threads (posix Pthreads, I believe), others can spawn another process using fork().
DOS has absolutely mo provisions for multithreading.  You could hook the timer interrupt and do some voodoo magic yourself but I advise against it.
0
 
strider031598Author Commented:
Yes, it is just a simple DOS program.  How can make other things happen while the program is waiting for input?  I think the timer example was a bad example because I don't care about synchronizing time.  Here's another example:  I know how to get input and change the background color, but how can I do both those actions simultaneously?
0
 
nietodCommented:
TMS has the right idea, but I don't think you need to go to the trouble of checking for keys in DOS's keyboard buffer.  (Which is a pain.)  The standard C++ library should have a way to check if there is data waiting in the input stream.  I don't use the standard C++ library for input and output (not much good in windows) so I don't know the details.
0
 
TMSCommented:
Well, I don't think there is a 'standard' way of doing this as this is highly OS dependant (under unix you would IOCTL to tweak terminal settings) But yes, my point was to plot the solution. Anyway, I think you are right nietod, there must be a RTL function related to DOS doing that...
Ans as I'm thinking, this comes back to my mind :) the function name is
kbhit()
it does check for keys awaiting in the keyboard buffer, so you can retrieve it with getch()
These functions were MS quickC, should work on MS C, I don't remember for Borland C (but I guess they are the same)

0
All Courses

From novice to tech pro — start learning today.