名前付きパイプを使用してプロセスと通信しています。私はそれを次のコードで動作させることができました。 (オリジナルのコードはここで見つける:via archive.org)コンソールからコマンドを発行しないC#Named Pipes?
class ProgramPipeTest
{
public void ThreadSenderStartClient(object obj)
{
// Ensure that we only start the client after the server has created the pipe
ManualResetEvent SyncClientServer = (ManualResetEvent)obj;
using (NamedPipeClientStream pipeStream = new NamedPipeClientStream(".","ToSrvPipe",PipeDirection.Out,PipeOptions.None))
{
// The connect function will indefinately wait for the pipe to become available
// If that is not acceptable specify a maximum waiting time (in ms)
pipeStream.Connect();
Console.WriteLine("[Client] Pipe connection established");
using (StreamWriter sw = new StreamWriter(pipeStream))
{
sw.AutoFlush = true;
string temp;
Console.WriteLine("Please type a message and press [Enter], or type 'quit' to exit the program");
while ((temp = Console.ReadLine()) != null)
{
if (temp == "quit") break;
sw.WriteLine(temp);
}
}
}
}
public void ThreadStartReceiverClient(object obj)
{
// Ensure that we only start the client after the server has created the pipe
ManualResetEvent SyncClientServer = (ManualResetEvent)obj;
using (NamedPipeClientStream pipeStream = new NamedPipeClientStream(".", "FromSrvPipe", PipeDirection.In, PipeOptions.None))
{
// The connect function will indefinately wait for the pipe to become available
// If that is not acceptable specify a maximum waiting time (in ms)
pipeStream.Connect();
Console.WriteLine("[ClientReceiver] Pipe connection established");
using (StreamReader sr = new StreamReader(pipeStream))
{
// Display the read text to the console
string temp;
while ((temp = sr.ReadLine()) != null)
{
Console.WriteLine("Received from server: {0}", temp);
}
}
}
}
static void Main(string[] args)
{
// To simplify debugging we are going to create just one process, and have two tasks
// talk to each other. (Which is a bit like me sending an e-mail to my co-workers)
ProgramPipeTest Client = new ProgramPipeTest();
Thread ClientThread = new Thread(Client.ThreadSenderStartClient);
Thread ReceivedThread = new Thread(Client.ThreadStartReceiverClient);
ClientThread.Start();
ReceivedThread.Start();
}
}
意図したとおりにすべてが動作します。ターゲットプロセス(大胆さ)にコマンドを発行することができます。
私の問題は、基本的にこのコードの周りにC#のGUIをラップしたいのですが、GUIを使ってコマンドを発行するため、コンソールを使わずに通信ができるように変更する方法がわかりません。コードから。
私はクラス変数に、プロパティを介して公開し、sw.WriteLine()をメソッドで呼び出すことを試みましたが、それは動作していないようです。
したがって、オブジェクト内でストリームをうまく前後にカプセル化する方法がわかりません。
私はこの記事が、Using Named Pipes to Connect a GUI to a Console App in Windowsにあったように見えましたが、残念ながら、コードが付いていないようで、参照することなく私の頭の上にあるようです。
コンソールを使用してコマンドを発行しなくても、名前付きパイプを使用するにはどうすればよいですか?
あなたを祝福してください。素晴らしい!これは完璧に動作します。 ToとFromのパイプの名前が同じではないため、別のpipeNameのコンストラクタに追加のパラメータを追加するだけでした。 – bulltorious
LOL、私はそれがどれほど完全であるかはわかりませんでしたが、それは同じ静脈の将来の問題の指針になることを意図していました。それはうまくいってうれしい! – Ani