Solved

Multiple Executions

Posted on 1998-05-06
9
288 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Building cUrl in Windows v7.43.0 4 45
Safe conversion? 4 88
FMX TEdit KeyUp handler detecting  "enter" key 4 64
parameter pack in c++11 2 23
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

710 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