Raise an event and begin listening to it (part 2)

Sorry fo this second thread about raising events, i opened a new one cause the first thread (http://www.cplusplus.com/forum/beginner/171441/) has been closed.

this was my initial request:

"Hello,
any simple way to raise an event and set a listener for such event?
I used to do this in actionscript with addEventListener and removeEventListener...
creating custom events and so on...
How to do this in c++?
thank you"

MiiNiPaa an JLBorges replied with some interesting informations:
http://coliru.stacked-crooked.com/a/1b9ec7059a4ff5f9

In addition to what asked in the previous thread i would like to ask if is is possibile to program an event at specific time, i.e. the event should fire in 15 mins.

Thank you very much


i would like to add
Something along these lines, perhaps:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include <iostream>
#include <chrono>
#include <thread>
#include <functional>
#include <ctime>

namespace detail
{
    template < typename LISTENER, typename... ARGS >
    static void do_raise_event( LISTENER listener,
                                unsigned int initial_delay_ms,
                                unsigned int period_ms, unsigned int repeat_cnt,
                                ARGS&&... args )
    {
        const auto fn = std::bind( listener, std::forward<ARGS>(args)... ) ;

        std::this_thread::sleep_for( std::chrono::milliseconds(initial_delay_ms) ) ;
        fn() ;

        for( unsigned int n = 0 ; n < repeat_cnt ; ++n )
        {
            std::this_thread::sleep_for( std::chrono::milliseconds(period_ms) ) ;
            fn() ;
        }
    }
}

template < typename LISTENER, typename... ARGS >
void raise_event( LISTENER listener,
                  unsigned int initial_delay_ms,
                  unsigned int period_ms, unsigned int repeat_cnt,
                  ARGS&&... args )
{
    std::thread( &detail::do_raise_event<LISTENER,ARGS...>,
                 listener,
                 initial_delay_ms,
                 period_ms, repeat_cnt,
                 std::forward<ARGS>(args)... ).detach() ;
}

const char* now()
{
    const std::time_t now = std::time(nullptr) ;
    return std::ctime( std::addressof(now) ) ;
}

void event_handler( int event_id )
{
    std::cout << "event #" << event_id << " raised at " << now() << '\n' << std::flush ;
}

int main()
{
    std::cout << "trigger event with 3 second delay at " << now() << '\n' << std::flush ;
    raise_event( event_handler, 3000, 1000, 2, 999 ) ;
    std::this_thread::sleep_for( std::chrono::seconds( 3 + 1*4 ) ) ;
}

http://coliru.stacked-crooked.com/a/ce73180644bc6245
Thank you very much, JLB....
...studying now

Will it work with c++11 too?
It won't work unless it's C++11 or above (which every sane person should at least be on). Why not try running it?
Last edited on
Topic archived. No new replies allowed.