是否有可能与正在运行的流程“交谈”?

我想创建一些将作为简单进程运行的服务,并将为其他应用程序提供向他发送xml流的可能性。

我的意思是使用无限循环创建简单的进程(exe) – 并且任何应用程序都能够将XML(文件/流)发送到此进程=>并且此进程将xml发送到某个套接字。

没有管道可以做到吗? 我想做一些类似COM的事情 – 可以“捕捉”工作流程的实例。

当然。

你可以在c#中使用命名管道类:

服务器:

using (var s = new NamedPipeServerStream ("myPipe")) { s.WaitForConnection(); s.WriteByte (100); Console.WriteLine (s.ReadByte()); } 

客户代码:

 using (var s = new NamedPipeClientStream ("myPipe")) { s.Connect(); Console.WriteLine (s.ReadByte()); s.WriteByte (200); } 

编辑

你可以通过文件来做。 + systemfileWatcher类

将文件放在一个文件夹中。

另一个进程将审核此文件夹。

现在你可以转移信息了。

EDIT2

你可以使用memoryMappedFile

并在每个过程中打开一个视图以查看相同的mempry区域 – 并传输数据。 我认为这是最好的。

流程A:

  static void Main(string[] args) { using (MemoryMappedFile mmf = MemoryMappedFile.CreateNew("testmap", 4000)) { bool mutexCreated; Mutex mutex = new Mutex(true, "testmapmutex", out mutexCreated); using (MemoryMappedViewStream stream = mmf.CreateViewStream()) { BinaryWriter writer = new BinaryWriter(stream); string st = "Hellow"; int stringSize = Encoding.UTF8.GetByteCount(st); //6 writer.Write(st); writer.Write(123); //6+4 bytes = 10 bytes } mutex.ReleaseMutex(); Console.WriteLine("Start Process B and press ENTER to continue."); Console.ReadLine(); mutex.WaitOne(); using (MemoryMappedViewStream stream = mmf.CreateViewStream()) { BinaryReader reader = new BinaryReader(stream); Console.WriteLine("Process A says: {0}", reader.ReadString()); Console.WriteLine("Process A says: {0}", reader.ReadInt32()); Console.WriteLine("Process B says: {0}", reader.ReadInt32()); } mutex.ReleaseMutex(); } } 

进程B写入其区域

  static void Main(string[] args) { try { using (MemoryMappedFile mmf = MemoryMappedFile.OpenExisting("testmap")) { Mutex mutex = Mutex.OpenExisting("testmapmutex"); mutex.WaitOne(); using (MemoryMappedViewStream stream = mmf.CreateViewStream(11, 0)) // From the 11 byte.... { BinaryWriter writer = new BinaryWriter(stream, Encoding.UTF8); writer.Write(2); } mutex.ReleaseMutex(); } } catch (FileNotFoundException) { Console.WriteLine("Memory-mapped file does not exist. Run Process A first."); } } 

只需使用C#套接字监听来自其他进程的连接并编写自定义XML文件接收器。

是的,当然您可以使用TCP 套接字连接。如果您想避免在注释中启发的网络连接,您可以使用共享内存方法,例如使用内存映射文件 。

您正在寻找的是某种forms的IPC(进程间通信)。 有很多可能性:

  1. 常规文件。 Windows提供专门用于临时文件的位置(%TEMP%)
  2. 对于小数据,您可以使用注册表,但在大多数情况下,它不适合使用
  3. 内存映射文件,它类似于文件但在RAM中
  4. 正如Royi正确提到的那样,如果您决定尝试使用管道,NamedPipeStream是一种可行的方法
  5. 您可以创建WCF端点。 这听起来像是一个拖累,但Visual Studio将创建你所有的脚手架,所以它最终不是一个问题
  6. 如果您正在开发表单应用程序,则可以使用窗口消息,有时即使不是
  7. 你提到数据是XML,所以这种方法不适合你,但我还是会提到它:你可以使用命名的内核对象,例如:互斥,事件,信号量来将信号从一个程序传递到另一个程序。