0

I have 2 simple applications the 1st one makes a string and the 2nd one reads the string..

App1 (ConsoleApp4.exe)

 for (int i = 0; i < 16; i++)
            {
                string x = " " + GetRandomString(6);
                Console.WriteLine("line " + (i + 1) + x);
                Thread.Sleep(3000);

            }

App2
  Process process = new Process();
            process.StartInfo.FileName = @"ConsoleApp4.exe";
            process.StartInfo.UseShellExecute = false;
            process.StartInfo.RedirectStandardOutput = true;
            process.OutputDataReceived += new DataReceivedEventHandler((sender, e) =>
            {
                // Prepend line numbers to each line of the output.
                if (!String.IsNullOrEmpty(e.Data))
                {
                    lineCount++;
                    //output.Append("\n[" + lineCount + "]: " + e.Data);
                    Console.WriteLine("\n[" + lineCount + "]: " + e.Data);
                }
            });

            process.Start();

            // Asynchronously read the standard output of the spawned process. 
            // This raises OutputDataReceived events for each line of output.
            process.BeginOutputReadLine();
            process.WaitForExit();

            // Write the redirected output to this application's window.
            Console.WriteLine(output);

            process.WaitForExit();
            process.Close();

            Console.WriteLine("\n\nPress any key to exit.");
            Console.ReadLine();

I want to change App1 to send an object instead of a string. The object would be

class Message
{
  string AMessage
  bool IsItTrue
  int MyFavoriteNumber
}

How can I alter App1 to send a custom object instead of a string. Then how can I alter App2 to read the custom object.

Also I would like to avoid Console.(anything) to send the message if possible

Nick LaMarca
  • 8,076
  • 31
  • 93
  • 152
  • I think you are basically looking for inter process communication, might find the answers here useful: https://stackoverflow.com/questions/528652/what-is-the-simplest-method-of-inter-process-communication-between-2-c-sharp-pro – peeyush singh Mar 26 '19 at 01:36

1 Answers1

2

Looking at the code provided, I will assume that these Apps are two separate console applications running in each other process.

There are several ways you could establish communication. Something I would strongly suggest you should take a look at are the named pipes. They provide a nice way to do exactly what you want.

Another approach that might work for you would be to use queues. A nice library for this is RabbitMQ.

I think that you can find enough examples of how to use them on the internet, so we don't have to write them here.

As for how to send an object instead of a string? In order to do that, you need to be able to serialize the object. A common way would be to serialize it to XML or JSON.

Hope this helps! Cheers and happy coding!

DasSoftware
  • 964
  • 10
  • 19