我有两个应用程序,分别是Cashier.exe和Payment.exe
我想将PosWindows.retrieveOrder
的数据从Cashier.exe传递到Payment.exe
PosWindows.retrieveOrder
包含大量数据,例如OrderId
,OrderCode
等(这意味着它不是单个数据)
我正在使用此代码,但是它不发送数据。是因为它不能发送整个数据吗?
ProcessStartInfo psi = new ProcessStartInfo();
psi.FileName = "C:/Project/Application/Payment/Payment.exe";
psi.Arguments = "\"" + PosWindows.totalAmount.ToString() + "\"\"" + PosWindows.retrieveOrder + "\"";
var p = Process.Start(psi);
如果我只发送PosWindows.totalAmount.ToString()
。
是这样的
ProcessStartInfo psi = new ProcessStartInfo();
psi.FileName = "C:/Project/Application/Payment/Payment.exe";
psi.Arguments = "\""+ PosWindows.totalAmount.ToString() + "\"";
var p = Process.Start(psi);
工作正常。但是当我添加PosWindows.retrieveOrder
时,它不起作用。
是否可以发送PosWindows.retrieveOrder
吗?
我不知道这个问题是否来自下面的代码(因为我没有声明retrieveOrder)
这是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);
}
}
}
如果是,我该怎么办?我的意思是我应该把decimal.Parse(arg)
的那一部分替换为retrieveOrder吗?
答案 0 :(得分:0)
您可以使用 NamedPipeServerStream 类。
将您的一个应用程序作为客户端,另一个作为服务器。侦听完成后,您可以处理异步通信并解析消息。
您还可以签出Example of Named Pipes
编辑解决方案示例:
在客户端应用上,让我们调用类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);
}
}
在初始化您的课程后,只需发送以下消息:
_pipeClient.Send(pipeMsg, "PipeName", Timeout);
在服务器应用程序中,让我们调用类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;
}
}
要处理管道流消息,请委派给处理程序并解析该消息:
_pipeServer.PipeMessage += new DelegateMessage(PipesMessageHandler);
在代码中所需的某个位置:
_pipeServer.Listen("PipeName");
并进行解析,例如:
private void PipesMessageHandler(string message)
{
if (this.Dispatcher.CheckAccess())
{
this.Dispatcher.Invoke(new NewMessageDelegate(PipesMessageHandler), message);
}
else
{
string pipeMessage = Convert.DateTime(message);
}
}