Is it possible to "speak" with a running process?

I want to create some service that will work as a simple process and give some other application the ability to send an xml stream to it.

I want to create a simple process (exe) with an Infinite loop - and any application can send XML (file / stream) to this process =>, and this process will send xml to some socket.

Can this be done without a pipe? I want to do something like COM - which can "catch" an instance of a workflow.

+3
source share
4 answers

for sure.

you can use Named Pipe classes in C #:

Server:

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

client code:

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

change

you can do it by file. + systemfileWatcher Class

put the file in a folder.

another process will check this folder.

and now you can transfer information.

edit2

you can use memoryMappedFile

and open a view in each process to see the same mempry region, and transfer data. I consider it the best.

Process 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(); } } 

Process B writes to its area

  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."); } } 
+5
source

Just use C # Sockets that listen on connections from another process and write a custom receiver of XML files.

+4
source

Yes, of course, you can use TCP socket . If you want to avoid network connectivity, as enlightened in the commentary, you can use a shared memory approach, such as Memory Files .

+3
source

What you are looking for is some form of IPC (interprocess communication). There are a huge number of possibilities:

  • Normal file. Windows provides a location specifically for temporary files (% TEMP%)
  • You can use the registry for small data, although in most cases this is a misnomer
  • A file with memory, similar to a file, but in RAM
  • As Royi correctly mentioned, NamedPipeStream is the way to go if you decide to try the pipes try
  • You can create a WCF endpoint. It sounds like a drag and drop, but Visual Studio will create all the forests for you, so in the end it's not a problem.
  • Window messages can be used if you are developing a forms application, and sometimes even if not
  • You mentioned that the data is XML, so this methodology is not for you, but I mentioned it all the same: you could use named kernel objects, such as mutexes, events, semaphores to transfer signals from one program to another.
+2
source

Source: https://habr.com/ru/post/922078/


All Articles