Link to home
Start Free TrialLog in
Avatar of strider031598
strider031598

asked on

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)?
Avatar of WxW
WxW

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.
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.
Yes sure I assume that it is a Windows Program

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.)
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.
Avatar of strider031598

ASKER

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?
ASKER CERTIFIED SOLUTION
Avatar of TMS
TMS

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.
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)