Recently debugging an algorithm to see how the results change by changing the parameters of the algorithm. The trouble is that it takes 15 minutes to load and build data from disk. This is more annoying, that is, I have to wait 15 minutes before each parameter to start time?
So I thought, could you open a datahost process that specifically loads the data. I debug parameters in another process, but I need data to load the ready-made data directly from the datahost process. So I just need to load the data once from the disk. So I looked for it, C # has a function called IPC (Inter process communication) that enables different interprocess communication on the same machine.
Notice my scenario here: I need interprocess communication "data Objects", not messages. Communication message is better to do, a string stream on the line. And what I need to pass is a data object, such as a dictionary, or a custom class.
The general process of IPC is this: the Datahost process first binds an object, the client accesses the object class based on the object's URI, and calls the method of that class. When the Datahost process receives a call from the client, it instantiates an object (note that it is time to instantiate an object, which is why I use static in My Code), executes the relevant function, and returns the result to the client. Note that the execution process is on the datahost side.
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;namespaceipcserver{ Public classDataobj:marshalbyrefobject { Public Staticdictionary<string,int>salary; Public Static stringCompany ; Public Static intcounter; Public Static intconstructercnt =0; Public Static voidLoadData () { Company="Microsoft"; Salary=Newdictionary<string,int>(); Salary. ADD ("LIANJX",3); Salary. ADD ("Uncle",5); Counter=0; } Publicdictionary<string,int>getsalary () {returndataobj.salary; } Publicdataobj () {dataobj.constructercnt++; Console.WriteLine ("Constructer ... {0}", dataobj.constructercnt); } }}
1 usingSystem;2 usingSystem.Collections.Generic;3 usingSystem.Linq;4 usingSystem.Runtime.Remoting;5 usingSystem.Runtime.Remoting.Channels;6 usingSystem.Runtime.Remoting.Channels.Ipc;7 usingSystem.Text;8 usingSystem.Threading.Tasks;9 Ten namespaceIpcserver One { A class Program - { - Static voidMain (string[] args) the { -Ipcserverchannel channel =NewIpcserverchannel ("Serverchannel"); -ChannelServices.RegisterChannel (channel,false); - Dataobj.loaddata (); +DATAOBJ.SALARY.ADD ("Jian", at); -DATAOBJ.SALARY.ADD ("Xun", A); +RemotingConfiguration.RegisterWellKnownServiceType (typeof(Dataobj),"Dataobj", WellKnownObjectMode.SingleCall); A atConsole.WriteLine ("Press AnyKey to exit"); - Console.readkey (); - } - } -}
1 usingIpcserver;2 usingSystem;3 usingSystem.Collections.Generic;4 usingSystem.Linq;5 usingSystem.Runtime.Remoting.Channels;6 usingSystem.Runtime.Remoting.Channels.Ipc;7 usingSystem.Text;8 usingSystem.Threading.Tasks;9 Ten namespaceipcclient One { A class Program - { - Static voidMain (string[] args) the { -Ipcclientchannel channel =NewIpcclientchannel (); -ChannelServices.RegisterChannel (channel,false); - +Dataobj obj =NULL; - + for(intt =0; T <1; t++) A { atConsole.WriteLine ("t={0}", T); - Try - { -obj = (dataobj) Activator.GetObject (typeof(Dataobj),"Ipc://serverchannel/dataobj"); - } - Catch(Exception e) in { - Console.WriteLine (e.message); to } + - if(obj = =NULL) the { *Console.WriteLine ("could not locate server"); $ }Panax Notoginseng Else - { the foreach(varPairinchobj. Getsalary ()) + { AConsole.WriteLine ("{0},{1}", pair. Key, pair. Value); the } +Console.WriteLine ("counter = {0}", obj. Getsalary ()); -dataobj.counter++; $ } $ } - -Console.WriteLine ("Mission complete!"); the Console.readkey (); - }Wuyi } the}
In fact, the results did not accelerate. It is also estimated that the IPC will be passed after the object is serialized and then deserialized. Sang-san-xin.
Next steps: Find a way to read the memory space of other processes directly ...
C # interprocess communication IPC