i continue with problems for use a callback function in a class member :(

class Timer
{
    private:
        typedef std::function<void(void)> timerprocedure;
        timerprocedure tmrprocedure;//can't be static
        int MilliSecondsTimer;
        bool blnDestroyed;
        UINT TimerId;
        HANDLE Timer1;
        static void CALLBACK MyTimerProc( HWND hwnd,  UINT message, UINT idTimer,  DWORD dwTime)
        {
            tmrprocedure();//call the lambda timer function
            //error: "invalid use of member 'Timer::tmrprocedure' in static member function"
        }
    public:

        int SetMilliSeconds(int MilliSeconds)
        {
            MilliSecondsTimer = MilliSeconds;
            return 0;
        }

        int GetMilliSeconds()
        {
            return (MilliSecondsTimer);
        }

        int Start(timerprocedure TimerProc)//receives the lambda timer function
        {

            tmrprocedure=TimerProc;
            TimerId = SetTimer(0, 0, MilliSecondsTimer,MyTimerProc);//activate the timer with timer callback procedure
            if(TimerId==NULL)
                MessageBox(NULL,"error","error",MB_OK);
            return 0;
        }

        int Stop()
        {
            KillTimer( 0,TimerId);
            return 0;
        }

        ~Timer()
        {
            Stop();
        }
};

the tmrprocedure needs to be static, but i don't want it static... so how can i pass the this class pointer with SetTimer() and how can i get the pointer in MyTimerProc() callback?

This issue here is because MyTimerProc is static, and must be static to be passed to the C WIN API Function SetTimer it has no this pointer i.e. it does not require or have an instance of the object. It is in effect a normal function. However tmrprocedure is member data and can only be accessed through a this pointer, i.e. it needs to have an instance of the object.

Every single one of your instanticated Timer classes will be calling the same callback, MyTimerProc, with no differentiation between what which instance of the class is being refered to.

However you do know which timer is caused the call because the timer id (which you store in class data, is passed to the callback.

The only way I can think of to get round this problem is to use a collection (say a map) to store the relationship between timer id and class instance in your Start function (e.g. std::map<TimerId, Timer*). Then you can use this map in MyTimerProc to look up the class instance from the timer id and call the right tmrprocedure.

Obviously since you will be using the collection in MyTimerProc you need to be able to do it without needing a class instance so your collection would need to be static member data which also means that if this is a multithreaded program that you will need to make access to the collect thread safe.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.