如何从控制台读取"不是主要"处理

时间:2016-11-21 15:09:54

标签: input concurrency erlang

如何在新进程中读取stdin?我可以把线和打印只在主过程中。我应该转到get_line控制台设备还是类似的,或者它不可能?

我的代码:

-module(inputTest).
-compile([export_all]).

run() ->
  Message = io:get_line("[New process] Put sth: "),
  io:format("[New process] data: ~p~n", [Message]).


main() ->
  Message = io:get_line("[Main] Put sth: "),
  io:format("[Main] data: ~p~n", [Message]),
  spawn(?MODULE, run, []).

1 个答案:

答案 0 :(得分:5)

问题是您的run/0进程会生成main/0然后立即退出。您应该run/0等待-module(inputTest). -compile([export_all]). run(Parent) -> Message = io:get_line("[New process] Put sth: "), io:format("[New process] data: ~p~n", [Message]), Parent ! {self(), ok}. main() -> Message = io:get_line("[Main] Put sth: "), io:format("[Main] data: ~p~n", [Message]), Pid = spawn(?MODULE, run, [self()]), receive {Pid, _} -> ok end. 完成。以下是您可以这样做的方法:

run/1

在产生run/1之后 - 并注意我们更改了它以将我们的进程ID传递给它 - 我们等待从它接收消息。在erl中,一旦我们打印到输出,我们就向父母发送一条消息,让它知道我们已经完成了。在1> inputTest:main(). [Main] Put sth: main [Main] data: "main\n" [New process] Put sth: run/1 [New process] data: "run/1\n" ok shell中运行它会产生以下结果:

public class MyClass<T> : Control where T : struct
{

    public T Value
    {
        get { return (T)GetValue(ValueProperty); }
        set { SetValue(ValueProperty, value); }
    }

    // DependencyProperty as the backing store for Value
    public static readonly DependencyProperty ValueProperty = DependencyProperty.Register(
        "Value",
        typeof(T),
        typeof(MyClass<T>),
        new PropertyMetadata(null, null, CoerceValue)
    );


    private static object CoerceValue(DependencyObject d, object baseValue)
    {
        // Check if value is valid
        return verifiedValue;
    }
}


public class MyDerivedClass : MyClass<int>
{

}