private async Task <PPError> CloseAsyncCore(MessageLoop messageLoop = null) { var tcs = new TaskCompletionSource <PPError>(); EventHandler <PPError> handler = (s, e) => { tcs.TrySetResult(e); }; try { HandleClose += handler; if (MessageLoop == null && messageLoop == null) { Close(); } else { Action <PPError> action = new Action <PPError>((e) => { PPBFileIO.Close(this); tcs.TrySetResult(PPError.Ok); } ); InvokeHelper(action, messageLoop); } return(await tcs.Task); } catch (Exception exc) { Console.WriteLine(exc.Message); tcs.SetException(exc); return(PPError.Aborted); } finally { HandleClose -= handler; } }
/// <summary> /// Close() cancels any IO that may be pending, and closes the FileIO object. /// Any pending callbacks will still run, reporting /// <code>Aborted</code> if pending IO was interrupted. It is not /// valid to call Open() again after a call to this method. /// /// <strong>Note:</strong> If the FileIO object is destroyed, and it is still /// open, then it will be implicitly closed, so you are not required to call /// Close(). /// </summary> public void Close() { PPBFileIO.Close(this); OnClose(PPError.Ok); }