private async Task <FileIOResult> WriteAsyncCore(byte[] buffer, int offset, int size, MessageLoop messageLoop = null) { var tcs = new TaskCompletionSource <FileIOResult>(); EventHandler <FileIOResult> handler = (s, e) => { tcs.TrySetResult(e); }; try { HandleWriteData += handler; if (MessageLoop == null && messageLoop == null) { Write(buffer, offset, size); } else { Action <PPError> action = new Action <PPError>((e) => { var result = (PPError)PPBFileIO.Write(this, offset, buffer, size, new BlockUntilComplete() ); tcs.TrySetResult(new FileIOResult((int)result, (int)result == 0)); } ); InvokeHelper(action, messageLoop); } return(await tcs.Task); } catch (Exception exc) { Console.WriteLine(exc.Message); tcs.SetException(exc); return(new FileIOResult((int)PPError.Aborted, true)); } finally { HandleWriteData -= handler; } }
/// <summary> /// Write() writes to an offset in the file. This function might perform a /// partial write. The FileIO object must have been opened with write access. /// </summary> /// <param name="buffer">The buffer to hold the specified number of bytes read.</param> /// <param name="offset">The offset into the file.</param> /// <param name="bytesToWrite">The number of bytes to write to /// <code>offset</code>.</param> /// <returns>Error code</returns> public PPError Write(byte[] buffer, int offset, int bytesToWrite) => (PPError)PPBFileIO.Write(this, offset, buffer, bytesToWrite, new CompletionCallback(OnWriteData));