[erlang-questions] how to handle a complex timer process ?

Kaiduan Xie kaiduanx@REDACTED
Mon Jun 14 20:17:07 CEST 2010


Info,

Erlang has already provided timer management, please check,

erlang:start_timer/erlang:cancel_timer

http://www.erlang.org/doc/man/erlang.html#start_timer-3

If you need a timer in gen_fsm, you can use gen_fsm:start_timer.

Kaiduan

On Mon, Jun 14, 2010 at 1:10 PM, info <info@REDACTED> wrote:
> Hi all,
> I want to manage a timer with several actions.
> - creation: creation of the timer process with "Period" as parameter. The timer shall be in suspend state.
> - start: the timer is started.
> - restart: the timer restarts with the Period.
> - stop: the timer is stopped. The timer shall be in wait state. A start starts the timer.
> - kill: the timer process is killed.
> - after the timeout, a message is sent to another process. The timer shall be in suspend state.
>
> For the creation, I do:
> Pid = spawn (fun,?MODULE,the_timer,[Period]),
> and after I manage like this:
> Pid ! {start}
> Pid ! {stop}
> Pid ! {restart}
> Pid ! {kill}
>
> The structure of the timer is like this:
>
> the_timer(Period)->
>    receive
>        {start}-> ???
>        {restart}->
>            the_timer(Period);
>        {stop}-> ???
>        {kill}-> ???
>    after Period ->
>        Pid0 -> {one_message},
>        ???
>    end.
>
> How to handle the state ? how to keep the time after a stop and to start again with this time ? how to kill the process ?
> How to start the timer only if a start message is received ?
>


More information about the erlang-questions mailing list