Is it possible to “talk” with running process?

后端 未结 4 407
悲哀的现实
悲哀的现实 2020-12-10 09:00

i want to create some service that will run as simple process and will give some other application the possibility to send him xml stream.

What i mean is to create

相关标签:
4条回答
  • 2020-12-10 09:38

    Just use C# Sockets that listen for connections from the other process and write a custom XML file receiver.

    0 讨论(0)
  • 2020-12-10 09:43

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

    edit

    you can do it by file. + systemfileWatcher Class

    put a file in a folder.

    the other process will audit this folder.

    and now you can transfer info.

    edit2

    you can use memoryMappedFile

    and open a view in each process to see the same mempry region - and transfer data. I think its 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 region

     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.");
                }
            }
    
    0 讨论(0)
  • 2020-12-10 09:50

    Yes, of course you can use a TCP socket connection .If you want to avoid network connection as enlightened in a comment you can use a shared memory approach, for example with Memory-Mapped Files .

    0 讨论(0)
  • 2020-12-10 09:56

    What you are looking for is some form of IPC (Inter-process communuication). There's a huge number of possibilities:

    1. Regular file. Windows provides location specifically for temp files (%TEMP%)
    2. For small data, you could use registry, although in most cases it's not a proper use
    3. Memory-mapped file, it's similar to file but in RAM
    4. As Royi properly mentioned, NamedPipeStream is a way to go if you decide to give pipes a try
    5. You could create a WCF endpoint. It sounds like a drag, but Visual Studio will create you all the scaffolding, so it's not such an issue in the end
    6. Window messages could be used if you are developing forms application, and sometimes even if not
    7. You mentioned that the data is XML, so this methodology is not for you, but I'll mention it anyway: you could use named kernel objects, such as: mutexes, events, semaphores to pass signals from one program to another.
    0 讨论(0)
提交回复
热议问题