Solved

Multiple Executions

Posted on 1998-05-06
9
253 Views
Last Modified: 2010-04-10
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)?
0
Comment
Question by:strider031598
  • 3
  • 2
  • 2
  • +2
9 Comments
 
LVL 6

Expert Comment

by:WxW
ID: 1173324
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
 
LVL 22

Expert Comment

by:nietod
ID: 1173325
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
 
LVL 6

Expert Comment

by:WxW
ID: 1173326
Yes sure I assume that it is a Windows Program

0
 
LVL 22

Expert Comment

by:nietod
ID: 1173327
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 11

Expert Comment

by:alexo
ID: 1173328
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
 

Author Comment

by:strider031598
ID: 1173329
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
 

Accepted Solution

by:
TMS earned 70 total points
ID: 1173330
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
 
LVL 22

Expert Comment

by:nietod
ID: 1173331
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
 

Expert Comment

by:TMS
ID: 1173332
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

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now