是否有可能"谈"与正在运行的进程? [英] Is it possible to "talk" with running process?

查看:169
本文介绍了是否有可能"谈"与正在运行的进程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一些服务将运行简单的过程,并给一些其他的应用程序来送他XML流的可能性。

我的意思是创建简单的过程(EXE)与无限循环 - 和任何应用程序将能够发送XML(文件/流)这一进程=>而这个过程将发送XML一些插座。

是否有可能做没有管? 我想要做类似COM - 工作过程中,可以捕获实例。

解决方案

确定。

您可以使用命名管道类在C#:<​​/ P>

服务器:

 使用(VAR S =新NamedPipeServerStream(myPipe))
{
 s.WaitForConnection();
 s.WriteByte(100);
 Console.WriteLine(s.ReadByte());
}
 

客户端code:

 使用(VAR S =新NamedPipeClientStream(myPipe))
{
 s.Connect();
 Console.WriteLine(s.ReadByte());
 s.WriteByte(200);
}
 

修改

您可以通过文件做到这一点。 + systemfileWatcher类

把文件中的文件夹中。

其他进程将审核该文件夹。

,现在你可以传送信息。

EDIT2

您可以使用memoryMappedFile

和每个进程中打开一个视图来查看同一mempry区域 - 和传输数据。  我认为它是最好的。

进程答:

 静态无效的主要(字串[] args)
        {
            使用(MemoryMappedFile MMF = MemoryMappedFile.CreateNew(testmap,4000))
            {
                布尔mutexCreated;
                互斥互斥=新的mutex(真的,testmapmutex,出mutexCreated);
                使用(MemoryMappedViewStream流= mmf.CreateViewStream())
                {
                    的BinaryWriter作家=新的BinaryWriter(流);
                    串ST =Hellow;
                    INT stringsize的= Encoding.UTF8.GetByteCount(ST); // 6
                    writer.Write(ST);
                    writer.Write(123); // 6 + 4字节= 10字节
                }
                mutex.ReleaseMutex();
                Console.WriteLine(启动进程B和preSS回车键继续。);
                到Console.ReadLine();
                mutex.WaitOne();
                使用(MemoryMappedViewStream流= mmf.CreateViewStream())
                {
                    读者BinaryReader在新= BinaryReader在(流);
                    Console.WriteLine(的处理的说:{0},reader.ReadString());
                    Console.WriteLine(的处理的说:{0},reader.ReadInt32());
                    Console.WriteLine(方法B说:{0},reader.ReadInt32());
                }
                mutex.ReleaseMutex();
            }
        }
 

进程B写入其区域

 静态无效的主要(字串[] args)
        {
            尝试
            {
                使用(MemoryMappedFile MMF = MemoryMappedFile.OpenExisting(testmap))
                {
                    互斥互斥= Mutex.OpenExisting(testmapmutex);
                    mutex.WaitOne();
                    使用(MemoryMappedViewStream流= mmf.CreateViewStream(11,0))//从11字节....
                    {
                        的BinaryWriter作家=新的BinaryWriter(流Encoding.UTF8);
                        writer.Write(2);
                    }
                    mutex.ReleaseMutex();
                }
            }
            赶上(FileNotFoundException异常)
            {
                Console.WriteLine(内存映射文件不存在运行处理的第。);
            }
        }
 

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.

解决方案

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

这篇关于是否有可能&QUOT;谈&QUOT;与正在运行的进程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆