/// <summary> /// Initializes a new instance of the FilePipe class /// </summary> /// <param name="name">the name of the target memory-mapped file</param> public FilePipe(string name) { if (!name.StartsWith(@"Global\")) { fileHandle = MemoryMappedFile.CreateOrOpen(name, bufferSize, MemoryMappedFileAccess.ReadWrite); mux = new InterProcessWaitHandle($"{name}_mx"); cmux = new InterProcessWaitHandle($"{name}_cmx"); mvs = fileHandle.CreateViewStream(); forEverClosed = new ManualResetEvent(false); } else { globalHandle = new GlobalMemoryMappedFile(name, bufferSize);//MemoryMappedFile.CreateOrOpen(name, bufferSize, MemoryMappedFileAccess.ReadWrite); mux = new InterProcessWaitHandle($"{name}_mx"); cmux = new InterProcessWaitHandle($"{name}_cmx"); mvs = globalHandle.ViewStream; forEverClosed = new ManualResetEvent(false); IsGlobal = true; } }
/// <summary>Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.</summary> public void Dispose() { if (listening) { listening = false; waiter.Cancel(); listener.GetAwaiter().GetResult(); listener = null; } mvs?.Dispose(); fileHandle?.Dispose(); globalHandle?.Dispose(); mux?.Dispose(); forEverClosed?.Dispose(); mvs = null; fileHandle = null; globalHandle = null; mux = null; forEverClosed = null; }