C # inter-process communication IPC,

Source: Internet
Author: User

C # inter-process communication IPC,

Recently I am debugging an algorithm. I want to change the parameters of the algorithm to see if the results have changed. It takes 15 minutes to load and build data from a disk. This is annoying, that is to say, I have to wait 15 minutes to start every time before calling a parameter?

So I was wondering if I could launch a datahost process to specifically load data. When I debug parameters in other processes but need to use data, I load the ready-made data directly from the datahost process. In this case, I only need to load data from the disk once. So I found out that c # has an inter process communication function that enables communication between different processes on the same machine.

Note my scenario here: I need to communicate "Data Objects" between processes, rather than messages. Communication messages are easy to handle. Just upload a string stream. What I need to pass is a data object, such as a Dictionary or custom class.

The general process of IPC is as follows: The datahost process first binds an object, and the client accesses the Object Class Based on the object uri and calls the method of the class. When the datahost process receives a call from the client, it will instantiate an object (note that an object is instantiated at this time, which is also the reason why static is used in my code ), execute related functions and return the results to the client. Note that the execution process is on the datahost side.

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace IPCServer{    public class DataObj : MarshalByRefObject    {        public static Dictionary<string, int> salary;        public static string company;        public static int counter;        public static int constructerCnt = 0;        public static void LoadData()        {            company = "Microsoft";            salary = new Dictionary<string, int>();            salary.Add("lianjx", 3);            salary.Add("uncle", 5);            counter = 0;        }        public Dictionary<string, int> GetSalary()        {            return DataObj.salary;        }        public DataObj()        {             DataObj.constructerCnt++;            Console.WriteLine("Constructer...{0}", DataObj.constructerCnt);        }    }}

using System;using System.Collections.Generic;using System.Linq;using System.Runtime.Remoting;using System.Runtime.Remoting.Channels;using System.Runtime.Remoting.Channels.Ipc;using System.Text;using System.Threading.Tasks;namespace IPCServer{    class Program    {        static void Main(string[] args)        {            IpcServerChannel channel = new IpcServerChannel("ServerChannel");            ChannelServices.RegisterChannel(channel, false);            DataObj.LoadData();            DataObj.salary.Add("jian", 23);            DataObj.salary.Add("xun", 22);            RemotingConfiguration.RegisterWellKnownServiceType(typeof(DataObj), "DataObj", WellKnownObjectMode.SingleCall);            Console.WriteLine("press anykey to exit");            Console.ReadKey();        }    }}
using IPCServer;using System;using System.Collections.Generic;using System.Linq;using System.Runtime.Remoting.Channels;using System.Runtime.Remoting.Channels.Ipc;using System.Text;using System.Threading.Tasks;namespace IPCClient{    class Program    {        static void Main(string[] args)        {            IpcClientChannel channel = new IpcClientChannel();            ChannelServices.RegisterChannel(channel, false);            DataObj obj = null;            for (int t = 0; t < 1; t++)            {                Console.WriteLine("t={0}", t);                try                {                    obj = (DataObj)Activator.GetObject(typeof(DataObj), "ipc://ServerChannel/DataObj");                 }                catch (Exception e)                {                    Console.WriteLine(e.Message);                }                if (obj == null)                {                    Console.WriteLine("could not locate server");                }                else                {                    foreach (var pair in  obj.GetSalary())                    {                        Console.WriteLine("{0},{1}", pair.Key, pair.Value);                     }                    Console.WriteLine("counter = {0}", obj.GetSalary());                    DataObj.counter++;                }            }            Console.WriteLine("Mission Complete!");            Console.ReadKey();        }    }}

In fact, the results are not accelerated. It is estimated that IPC also transmits the object after serialization and deserializes it. Sang Xin.

Next steps: Find a way to directly read the memory space of other processes...

 

Go to: http://www.cnblogs.com/sylvanas2012/p/4294393.html

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.