wpf - Inter-process communication sample-server & client C# -
using .net 4, wpf c#, passing method return values , parameters between 2 processes.
as need connection open , active times, have tried best minimize code recurring (within loop) unless put whole code inside loop did not succeed (after first transfer connection server closed), here, work repeatedly.
i wondering first way should coded, process including new instance, dispose, close... within loop?
is available datatype inter-process communication pass string (inefficient)?
public void client() { (int = 0; < 2; i++) { system.io.pipes.namedpipeclientstream pipeclient = new system.io.pipes.namedpipeclientstream(".", "testpipe", system.io.pipes.pipedirection.inout, system.io.pipes.pipeoptions.none); if (pipeclient.isconnected != true) { pipeclient.connect(550); } system.io.streamreader sr = new system.io.streamreader(pipeclient); system.io.streamwriter sw = new system.io.streamwriter(pipeclient); string status; status = sr.readline(); if (status == "waiting") { try { sw.writeline("param1filename.cs,33" + i); sw.flush(); pipeclient.close(); } catch (exception ex) { throw ex; } } } } public string server() { namedpipeserverstream pipeserver = null; { try { pipeserver = new namedpipeserverstream("testpipe", pipedirection.inout, 4); streamreader sr = new streamreader(pipeserver); streamwriter sw = new streamwriter(pipeserver); system.threading.thread.sleep(100); pipeserver.waitforconnection(); string test; sw.writeline("waiting"); sw.flush(); pipeserver.waitforpipedrain(); test = sr.readline(); if (!string.isnullorempty(test)) try { system.windows.application.current.dispatcher.invoke(new action(() => mbxtw.show(convert.toint32(test.split(',')[1]), test.split(',')[0], "method()", "warning!! - " + "content")), system.windows.threading.dispatcherpriority.normal); } catch (exception e) { } } catch (exception ex) { throw ex; } { pipeserver.waitforpipedrain(); if (pipeserver.isconnected) { pipeserver.disconnect(); } } } while (true); }
after thorough research on inter process communication have changed approach using named pipes memory mapped files, rounds winner, no need recreate , faster
i present ultimate partitioned-global-app-inter-process communication
any thoughts on code appreciated!
public class mmfintercomt { public eventwaithandle flagcaller1, flagcaller2, flagreciver1, flagreciver2; private system.io.memorymappedfiles.memorymappedfile mmf; private system.io.memorymappedfiles.memorymappedviewaccessor accessor; public virtual string depositchlname { get; set; } public virtual string depositthrdname { get; set; } public virtual int depositsize { get; set; } private system.threading.thread writerthread; private bool writerthreadrunning; public int readposition { get; set; } public list<string> statusset; private int writeposition; public int writeposition { { return writeposition; } set { if (value != writeposition) { this.writeposition = value; this.accessor.write(writeposition + read_confirm_offset, true); } } } private list<byte[]> datatosend; private const int data_available_offset = 0; private const int read_confirm_offset = data_available_offset + 1; private const int data_length_offset = read_confirm_offset + 1; private const int data_offset = data_length_offset + 10; public ipcmmfintercomsf.mmfintercomtstatus intercomstatus; public mmfintercomt(string ctripcchannelnamestr, string ctripcthreadname, int ctrmmfsize) { this.depositchlname = ctripcchannelnamestr; this.deposit size = ctrmmfsize; this.depositthrdname = ctripcthreadname; mmf = memorymappedfile.createoropen(depositchlname, depositsize); accessor = mmf.createviewaccessor(0, depositsize, system.io.memorymappedfiles.memorymappedfileaccess.readwrite);//if (started) //smlock = new system.threading.mutex(true, ipcmutxname, out locked); readposition = -1; writeposition = -1; this.datatosend = new list<byte[]>(); this.statusset = new list<string>(); } public bool reading; public byte[] readdata; public void startreader() { if (this.intercomstatus != ipcmmfintercomsf.mmfintercomtstatus._null || readposition < 0 || writeposition < 0) return; this.intercomstatus = ipcmmfintercomsf.mmfintercomtstatus.preparingreader; system.threading.thread t = new system.threading.thread(readerthread); t.isbackground = true; t.start(); } private void readerthread(object stateinfo) { // checks if there read. this.intercomstatus = ipcmmfintercomsf.mmfintercomtstatus.tryingtoread; this.reading = accessor.readboolean(readposition + data_available_offset); if (this.reading) { this.intercomstatus = ipcmmfintercomsf.mmfintercomtstatus.readingdata; // checks how many bytes read. int availablebytes = accessor.readint32(readposition + data_length_offset); this.readdata = new byte[availablebytes]; // reads byte array. int read = accessor.readarray<byte>(readposition + data_offset, this.readdata, 0, availablebytes); // sets flag used signal there aren't available data anymore. accessor.write(readposition + data_available_offset, false); // sets flag used signal data has been read. accessor.write(readposition + read_confirm_offset, true); this.intercomstatus = ipcmmfintercomsf.mmfintercomtstatus.finishedreading; } else this.intercomstatus = ipcmmfintercomsf.mmfintercomtstatus._null; } public void write(byte[] data) { if (readposition < 0 || writeposition < 0) throw new argumentexception(); this.statusset.add("readwrite:-> " + readposition + "-" + writeposition); lock (this.datatosend) this.datatosend.add(data); if (!writerthreadrunning) { writerthreadrunning = true; writerthread = new system.threading.thread(writerthread); writerthread.isbackground = true; writerthread.name = this.depositthrdname; writerthread.start(); } } public void writerthread(object stateinfo) { while (datatosend.count > 0 && !this.disposed) { byte[] data = null; lock (datatosend) { data = datatosend[0]; datatosend.removeat(0); } while (!this.accessor.readboolean(writeposition + read_confirm_offset)) system.threading.thread.sleep(133); // sets length , write data. this.accessor.write(writeposition + data_length_offset, data.length); this.accessor.writearray<byte>(writeposition + data_offset, data, 0, data.length); // resets flag used signal data has been read. this.accessor.write(writeposition + read_confirm_offset, false); // sets flag used signal there data avaibla. this.accessor.write(writeposition + data_available_offset, true); } writerthreadrunning = false; } public virtual void close() { if (accessor != null) { try { accessor.dispose(); accessor = null; } catch { } } if (this.mmf != null) { try { mmf.dispose(); mmf = null; } catch { } } disposed = true; gc.suppressfinalize(this); } private bool disposed; } and usage
instaciant once !
public static bool startcurprojintercom(ipcaccessorsetting cursrv, int deposize) { if(curprojmmf ==null) curprojmmf = new mmfintercomt(cursrv.channel.tostring(), cursrv.accthreadname.tostring(), deposize); curprojmmf.flagcaller1 = new eventwaithandle(false, eventresetmode.manualreset, curprojmmf.depositthrdname); curprojmmf.flagcaller2 = new eventwaithandle(false, eventresetmode.manualreset, curprojmmf.depositthrdname); curprojmmf.flagreciver1 = new eventwaithandle(false, eventresetmode.manualreset, ipcaccessorthreadnames.debuggerthrd.tostring()); curprojmmf.readposition = cursrv.accessorsectorssets.deposects.setter.read; curprojmmf.writeposition = cursrv.accessorsectorssets.deposects.setter.write; console.writeline("mmfintercomsetter.readposition " + curprojmmf.readposition); console.writeline("mmfintercomsetter.writeposition " + curprojmmf.writeposition); curprojmmf.startreader(); return true; } use many
public static void startadebugerintercomcall(ipccarier setterdataobj) { ipcaccessorsetting cursrv = new ipcaccessorsetting(ipcmmf.ipchannels.debugger, ipcaccessorthreadnames.debuggerthrdcurproj, 0, 5000); startcurprojintercom(cursrv, 10000); var dataw = setterdataobj.ipccariertobytearray();//system.text.encoding.utf8.getbytes(msg); curprojmmf.write(dataw); curprojmmf.flagreciver1.set(); curprojmmf.flagcaller1.waitone(); curprojmmf.flagcaller1.reset(); }
Comments
Post a Comment