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

黃耀賢 (Yau-Hsien Huang) <>
Mon Jun 14 20:31:27 CEST 2010


On Tue, Jun 15, 2010 at 1:10 AM, info <> wrote:

> 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 ?
>

Do not think about how to keep all the state. Think simple things.

Part of my solution is:

the_timer(Period, N) ->
    receive
start ->
    receive
restart ->
    ....................
stop ->
    the_timer(Period, N)
    after 1000 ->
    io:format("~w~n", [N]),
    .......... ! start,
    the_timer(Period, .............)
    end;
kill ->
    ok
    after Period ->
    io:format("I\'m waiting ...~n"),
    ........................
    end.


More information about the erlang-questions mailing list