Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
141 views
in Technique[技术] by (71.8m points)

c# - Is it possible to "talk" with running process?

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 simple process ( exe ) with Infinite loop - and any application will be able to send XML ( file / stream ) to this process => and this process will send the xml to some socket.

Is it possible to do it without pipe ? I want to do something like COM - that can 'catch' instance of working process.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...