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?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

0
Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

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
TMSCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.