Bonjour,

J'ai une classe appelé Countdown qui gère un compte à rebours. Pour cela, j'utilise boost::asio::deadline_timer en asynchrone.
J'aimerai passer en paramètre de mon constructeur la fonction a déclencher lorsque le compte à rebours a expiré et démarrer le compte à rebours par une fonction start.

Mon problème est que je n'arrive pas à déclencher la fonction passée en paramètre.

Voici le code:

Countdown.hpp
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
#ifndef COUNTDOWN_HPP_
#define COUNTDOWN_HPP_
 
#include <boost/bind.hpp>
#include <boost/asio.hpp>
#include <boost/function.hpp>
 
class Countdown
{
private:
	bool m_actived;
	bool m_suspended;
	boost::asio::deadline_timer m_runTimer;
	boost::function<void(const boost::system::error_code& error)> m_handler;
 
public:
	Countdown(boost::asio::io_service& ioService,boost::function<void(const boost::system::error_code& error)> handler);
	~Countdown();
	void start();
	void suspend();
	void stop();
	void setValue(short value);
};
 
 
#endif /* COUNTDOWN_HPP_ */
Countdown.cpp
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
#include "Countdown.hpp"
 
Countdown::Countdown(boost::asio::io_service& ioService,boost::function<void(const boost::system::error_code& error)> handler)
: m_actived(true),m_suspended(false),m_runTimer(ioService),m_handler(handler)
{
	m_runTimer.expires_from_now(boost::posix_time::seconds(1));
}
 
Countdown::~Countdown()
{
	m_actived = false;
	m_runTimer.cancel();
}
 
void Countdown::start()
{
	m_actived = true;
	m_runTimer.async_wait(m_handler);
}
 
void Countdown::suspend()
{
	if(m_suspended)
	{
 
	}
	else
	{
 
	}
}
 
void Countdown::stop()
{
	m_actived = false;
	m_runTimer.cancel();
}
 
void Countdown::setValue(short value)
{
	m_runTimer.expires_from_now(boost::posix_time::seconds(value));
}
main.cpp
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
#include <iostream>
#include <windows.h>
 
#include "Countdown.hpp"
 
void expireHandler(const boost::system::error_code& error)
{
	std::cout << "end of the countdown" << std::endl;
}
 
int main() {
	boost::asio::io_service ioService;
	boost::system::error_code error;
	Countdown countdown(ioService,
			boost::bind(
					&expireHandler,
					error
	));
	countdown.setValue(5);
	std::cout << "Start of the countdown" << std::endl;
	countdown.start();
	Sleep(10000);
	std::cout << "end of the program" << std::endl;
	return 0;
}
Résultat
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
Start of the countdown
end of the program