Solved

Call Back notification

Posted on 1998-12-13
4
201 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
[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
  • 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

Technology Partners: 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!

Question has a verified solution.

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

Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
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.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.

734 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