Solved

Call Back notification

Posted on 1998-12-13
4
199 Views
Last Modified: 2010-04-15
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!
0
Comment
Question by:hshliang
  • 2
4 Comments
 

Accepted Solution

by:
Mithander earned 100 total points
ID: 1255092
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
 
LVL 10

Expert Comment

by:RONSLOW
ID: 1255093
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
 

Author Comment

by:hshliang
ID: 1255094
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
 
LVL 10

Expert Comment

by:RONSLOW
ID: 1255095
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

Featured Post

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.

813 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

19 Experts available now in Live!

Get 1:1 Help Now