1

I have two application which is Cashier.exe and Payment.exe

I want to pass the data for PosWindows.retrieveOrder from Cashier.exe to Payment.exe

PosWindows.retrieveOrder contains lots of data such as OrderId, OrderCode and more (which means its not single data)

I'm using this code but it does not send the data. is it because it cannot send a whole data like that ?

ProcessStartInfo psi = new ProcessStartInfo();
                    psi.FileName = "C:/Project/Application/Payment/Payment.exe";
                    psi.Arguments = "\"" + PosWindows.totalAmount.ToString() + "\"\"" + PosWindows.retrieveOrder + "\"";
                    var p = Process.Start(psi);

If I only send PosWindows.totalAmount.ToString().

which is something like this

 ProcessStartInfo psi = new ProcessStartInfo();
                    psi.FileName = "C:/Project/Application/Payment/Payment.exe";
                    psi.Arguments = "\""+ PosWindows.totalAmount.ToString() + "\"";
                    var p = Process.Start(psi);

its working fine. but when I add PosWindows.retrieveOrder its not working.

<code>PosWindows.retrieveOrder</code> is not empty for sure

does it impossible to send the PosWindows.retrieveOrder ?

I don't know if this problem come from this code below (because I don't declare for retrieveOrder)

This one at Payment.exe

 private void app_Startup(object sender, StartupEventArgs e)
    {
        var args = e.Args;
        if (args != null && args.Count() > 0)
        {
            foreach (var arg in args)
            {
                PaymentView.globalTotalAmount = decimal.Parse(arg);


            }
        }
    }

if yes what will I do ? I means what should I put to replace this part decimal.Parse(arg) for retrieveOrder ?

rayna qarla
  • 93
  • 2
  • 11

1 Answers1

0

You can use NamedPipeServerStream class.

https://learn.microsoft.com/en-us/dotnet/api/system.io.pipes.namedpipeserverstream?view=netframework-4.7.2

Take one of your apps as client, and the other as server. You can handle an async communication and parse your message when listening is completed.

Also you can check out Example of Named Pipes

Edit for solution example:

At client app, lets call the class PipeClient.cs

    public void Send(string SendStr, string PipeName, int TimeOut = 1000)
    {
        try
        {
            NamedPipeClientStream pipeStream = new NamedPipeClientStream(".", PipeName, PipeDirection.Out, PipeOptions.Asynchronous);

            // The connect function will indefinitely wait for the pipe to become available
            // If that is not acceptable specify a maximum waiting time (in ms)
            pipeStream.Connect(TimeOut);
            Debug.WriteLine("[Client] Pipe connection established");

            byte[] _buffer = Encoding.UTF8.GetBytes(SendStr);
            pipeStream.BeginWrite(_buffer, 0, _buffer.Length, AsyncSend, pipeStream);
        }
        catch (Exception ex)
        {
            Debug.WriteLine("Pipe Send Exception: " + ex);
        }
    }


    private void AsyncSend(IAsyncResult iar)
    {
        try
        {
            // Get the pipe
            NamedPipeClientStream pipeStream = (NamedPipeClientStream)iar.AsyncState;

            // End the write
            pipeStream.EndWrite(iar);
            pipeStream.Flush();
            pipeStream.Close();
            pipeStream.Dispose();
        }
        catch (Exception oEX)
        {
            Debug.WriteLine(oEX.Message);

        }
    }

And after you initialize your class just send the message with:

_pipeClient.Send(pipeMsg, "PipeName", Timeout);

At server app, lets call the class PipeServer.cs

    public void Listen(string PipeName)
    {
        try
        {
            // Set to class level var so we can re-use in the async callback method
            _pipeName = PipeName;
            // Create the new async pipe 
            NamedPipeServerStream pipeServer = new NamedPipeServerStream(PipeName, PipeDirection.In, 1, PipeTransmissionMode.Message, PipeOptions.Asynchronous);

            // Wait for a connection
            pipeServer.BeginWaitForConnection(new AsyncCallback(WaitForConnectionCallBack), pipeServer);
        }
        catch (Exception oEX)
        {
            Debug.WriteLine(oEX.Message);
        }
    }

    private void WaitForConnectionCallBack(IAsyncResult iar)
    {
        NamedPipeServerStream pipeServer = (NamedPipeServerStream)iar.AsyncState;
        try
        {
            // End waiting for the connection
            pipeServer.EndWaitForConnection(iar);

            byte[] buffer = new byte[255];

            // Read the incoming message
            pipeServer.Read(buffer, 0, 255);

            // Convert byte buffer to string
            string stringData = Encoding.UTF8.GetString(buffer, 0, buffer.Length);
            Debug.WriteLine(stringData + Environment.NewLine);

            // Pass message back to calling form
            PipeMessage.Invoke(stringData);

            // Kill original server and create new wait server
            pipeServer.Close();
            pipeServer = null;
            pipeServer = new NamedPipeServerStream(_pipeName, PipeDirection.In, 1, PipeTransmissionMode.Message, PipeOptions.Asynchronous);

            // Recursively wait for the connection again and again....
            pipeServer.BeginWaitForConnection(new AsyncCallback(WaitForConnectionCallBack), pipeServer);
        }
        catch (Exception ex)
        {
            string ctch = ex.ToString();
            return;
        }
    }

For handling the pipestream message, delegate to a handler and parse the message:

_pipeServer.PipeMessage += new DelegateMessage(PipesMessageHandler);

at somewhere you need in your code:

_pipeServer.Listen("PipeName");

and parse for example:

private void PipesMessageHandler(string message) 
{
    if (this.Dispatcher.CheckAccess())
    {
        this.Dispatcher.Invoke(new NewMessageDelegate(PipesMessageHandler), message);
    }
    else
    { 
        string pipeMessage = Convert.DateTime(message);
    }
}
Spellblast
  • 21
  • 1
  • 5
  • 1
    Would be nice if you create an example of your solution here, instead of referring to a link. – Isma Mar 29 '19 at 16:23
  • thank you for suggestion. but I don't want to use the named pipes. If possible I want to do the way like I do. and yes it would be nice if you create an example of your solution here – rayna qarla Apr 01 '19 at 01:30