0

I am trying to communicate between 2 programs but I can’t find out how.

Process p = Process.Start(Secondprogram.exe, "number = " + number);
p.WaitForExit();
this.Close();

I want to receive the number in my second program and use it there.

Or something like:

Process p = Process.Start(Secondprogram.exe);
Parameter number = 1200;
p.WaitForExit();
this.Close();

And then in my second program:

txtSomething.Text = number;
poke
  • 369,085
  • 72
  • 557
  • 602
robbii15
  • 33
  • 1
  • 8

2 Answers2

3

You have several options:

  • Using WCF will allow you to switch between bindings easily. So if you have two processes on the same machine you can use a named pipe binding which is a type of inter-process communication. If you later decide to separate processes onto different machines you can just change configuration to, say, tcp binding and everything will continue working

  • If you are into REST, there will be client-server frameworks, such as as self-hosted ASP NET Web API or Nancy

  • If it is something dead simple, you can just write to a file and read from a file, combined with a global named mutex to synchronise access

  • There is also a choice of a memory-mapped file, where one process writes to a file and the other reads from it. It's a bit geeky approach and I have rarely seen this in use.

  • Or yet another conventional approach is to use a 3-rd party, such as a database or a message queue system

oleksii
  • 35,458
  • 16
  • 93
  • 163
1

You can use the output stream of the first program as input stream of the second one. Check ProcessStartInfo : https://msdn.microsoft.com/fr-fr/library/system.diagnostics.processstartinfo%28v=vs.110%29.aspx

norisknofun
  • 859
  • 1
  • 8
  • 24
  • This is an interesting approach. Could you provide an example? I have exacly this kind of situtaiton where I start another process with ProcessStartInfo. Would it also work if I don't wait for the started process to exti? – t3chb0t Feb 27 '16 at 21:40