Event handling using callback functions

Discussion in 'C' started by priyapai, Aug 14, 2007.

  1. priyapai

    priyapai New Member

    Joined:
    Mar 24, 2006
    Messages:
    13
    Likes Received:
    0
    Trophy Points:
    0
    Hi All,

    I am not vry clear with the usage of callback function for Event handling.
    What i understand is that, the Application registers a callback function. This callback func has the code to handle the diffent events that are expected. When one of these events hapen, the Callbak func gets called. But what i want to understand is that, how does the callback function gets to know that it has to get executed? If the same event is handled by two diff functons, then what should happen..

    Plz send me links or pointers wher i can study mor about the same topic.

    Thanks in advance.
     
  2. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    When the function is called means the function executes and the callee who provide the function callback knows when to call the function call back.
     
  3. priyapai

    priyapai New Member

    Joined:
    Mar 24, 2006
    Messages:
    13
    Likes Received:
    0
    Trophy Points:
    0
    Thankx for the quick responce Shabbir.

    But m still not clear.

    int (*function_cb) (arg1, arg2)

    return_val = caller_function (arg11,arg22,(int)function_cb);


    In the above case, when will function_cb get called?
     
  4. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    function_cb is a function pointer and it will not get call but depending on the situation the callee will call the function (pointed by the function pointer)

    I will give you an example.
    1. Say you have a function which updates the progress bar for some copy of files operation.
    2. When any chunk of file is copied successfully you call the function.
    3. Function does it job to update the progress bar.
     
  5. priyapai

    priyapai New Member

    Joined:
    Mar 24, 2006
    Messages:
    13
    Likes Received:
    0
    Trophy Points:
    0
    ok..

    Could u give me an example in code form, which handles some event by calling a callback function.
     
  6. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    Refer to the API CopyFileEx where you can specify your call back function in the third parameter and you will see how the call back function of yours is called
     
  7. kaustubh

    kaustubh New Member

    Joined:
    Aug 13, 2007
    Messages:
    25
    Likes Received:
    0
    Trophy Points:
    0

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice