• C

Call Back notification

I want to write a sort of Call Back fucntion to notifiy a caller the progress of the function.

Like:  main(){
          callfunc();

        ???some call back to notify this application about progress???

       }

       int callfunc(){
           while{
           //Do something that takes time
           CallBack function() //to tell appliction it has                                //done one step
           }
        }

This will eventually be use in MFC (Windows 95) application to display the progress bar.

Help is appreciated, but please tell me how to write Call Back function or any other simpler or easier ways.



Thanks a million!
hshliangAsked:
Who is Participating?
 
MithanderCommented:
The easiest way I can see to do it is like this:

float perDone;
int isDone = 0;

void main(void)
{
  while(!isDone)
  {
    callfunc();
    //Display perDone here
  }
}

callfunc()
{
  if(whatever your waiting for is done)
  {
    isDone=1;
    return;
  }
  //Do what you need here
  //Calculate % done and save it in perDone;
}

Another way to do it is to just display the things you want from callfunc().  Also you could do it using and MFC timer.  I've done this before, but I don't have a compiler handy, so I'll just use psudo code.

double perDone;

main(){ callfunc(); }

callfunc()
{
  Start timer(10 ms);
  while(....){  Update perDone, do what you want etc. }
}

OnTimer()
{
  //this will execute once everytime the clock timer runs out.  In this case 10 ms.
  //that means it needs to be somewhat fast, if it runs 10 times a sec and
  //redraws the whole screen, then your program will be much slower
  Update perDone;
}

Those are the way's I can think of doing it.  Let me know if you what more help with it.
0
 
RONSLOWCommented:
main(){
  callfunc();
}

void callback() {
  ???some call back to notify this application about progress???
}

int callfunc(){
  while{
    //Do something that takes time
    CallBack() //to tell appliction it has
               //done one step
  }
}

If you really want main to get a look-in on every iteration, then I'd suggest putting the loop itself into main and have callfunc do a single iteration only.

In windows things are quite different as it is multi-threaded and you can sent messages etc.  so this sort of thing can be a bit easier.

0
 
hshliangAuthor Commented:
I have seen passing a function pointer to the loop, but I don't know if that is the best way. (like in Windows CallBack Function). Your first way involves the use of global variables, which may not be a good idea if I port it to C++. the second way related more to time but not percent done. I prefer the first way, but any though of avoiding global variables? As when I port it to a C++ class, it will not work. Thnaks.
0
 
RONSLOWCommented:
have you function return a value (like a bool) rather than setting a global variable.

r you can pass a pointer a function (or variable) to call (or modify) in your function.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.