Last active
April 8, 2016 17:50
-
-
Save wtarr/779ba0180de92ece8936b7b8cae2654e to your computer and use it in GitHub Desktop.
Elixir to C# port example, based on http://stackoverflow.com/a/25342599
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule PortExample do | |
def start_exe(exe_name, arguments) do | |
# start the executable process for what settings mean see http://erlang.org/doc/man/erlang.html#open_port-2 | |
port = Port.open({:spawn_executable, exe_name}, [{:args, arguments}, :stream, :binary, :exit_status, :hide, :use_stdio, :stderr_to_stdout]) | |
# spawn a listening process | |
handle_output(port) | |
# return the port | |
port | |
end | |
def handle_output(port) do | |
spawn(__MODULE__, :listen, [port]) | |
end | |
def listen(port) do | |
receive do | |
{^port, {:data, data}} -> | |
IO.puts(data) | |
listen(port) | |
{^port, {:exit_status, status}} -> | |
status | |
end | |
end | |
end | |
# // date.exe console app that echos message along with date | |
# | |
# class Program | |
# { | |
# static void Main(string[] args) | |
# { | |
# string input = ""; | |
# while (true) | |
# { | |
# input = Console.ReadLine(); | |
# if (input == "exit") break; | |
# Console.Out.WriteLine($"{ DateTime.UtcNow} - {input}"); | |
# } | |
# Console.WriteLine("exiting ..."); | |
# } | |
# } | |
# *** on the iex command line *** | |
# | |
# c("portexample.ex") | |
# port = PortExample.start_exe("date", []) | |
# Port.command(port, "message from elixir\n") | |
# | |
# flush to see message |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment