public void Dispose() { if (FormUI != null) { FormUI.FormClosed -= UserInterface_Closed; FormUI.Close(); FormUI = null; } else if (WindowUI != null) { WindowUI.Closed -= UserInterface_Closed; WindowUI.Close(); WindowUI = null; } else if (Instance != null) { Instance.Dispose(); } IEnumerable <TaskCompletionSource <HPacket> > handledDataSources = DataAwaiters.Values.ToArray(); foreach (TaskCompletionSource <HPacket> handledDataSource in handledDataSources) { handledDataSource.SetResult(null); } Instance = null; CurrentState = DISPOSED_STATE; }