private async Task dispose(IPCModuleDisposeReason reason, Exception e = null) { await disposeLock.WaitAsync().ConfigureAwait(false); try { var state = State; if (state == IPCModuleState.Disposing || state == IPCModuleState.Disposed) { return; } State = IPCModuleState.Disposing; DisposeReason = reason; } finally { disposeLock.Release(); } cancel?.Cancel(); if (receiveTask != null) { try { await receiveTask.ConfigureAwait(false); } catch (EndOfStreamException) { //切断されていても無視 } catch (IOException) { //IOエラーがあっても無視 } finally { receiveTask = null; } } tunnel?.Dispose(); tunnel = null; State = IPCModuleState.Disposed; MessageReceivedSynchronizationContext.Post(_ => Disposed?.Invoke(e), null); }
/// <summary> /// <see cref="IPCModule"/>を初期化する /// </summary> /// <param name="messageReceivedSynchronizationContext"><see cref="MessageReceived"/>を呼び出す同期コンテキスト /// <para><seealso cref="SynchronizationContext.Send"/>や<seealso cref="SynchronizationContext.Post"/>された処理を排他的に実行するものである必要がある</para></param> public IPCModule(string sendPipeName, string receivePipeName, SynchronizationContext messageReceivedSynchronizationContext) { this.State = IPCModuleState.BeforeConnect; this.tunnel = new IPC.IPCTunnel(sendPipeName, receivePipeName); this.MessageReceivedSynchronizationContext = messageReceivedSynchronizationContext; }