public void Dispose() { lock (_locker) { if (_nativeXps != null) { Task.Run(async() => { while (!CanDispose) { await Task.Delay(2000); } try { _nativeXps.Dispose(); _nativeXps = null; _dataHandle.Free(); } catch { } }); } } }
public TilesManager(Stream xpsStream) { if (xpsStream == null) { return; } var errorBuffer = new byte[256]; var errorHandle = GCHandle.Alloc(errorBuffer, GCHandleType.Pinned); var dataBuffer = StreamToByteArray(xpsStream); _dataHandle = GCHandle.Alloc(dataBuffer, GCHandleType.Pinned); try { lock (_locker) { if (_nativeXps != null) { throw new RenderException("Already initialized"); } var pageCount = 0; var xpsNativePtr = RenderLibrary.open_xps_with_stream( _dataHandle.AddrOfPinnedObject(), dataBuffer.Length, ref pageCount, errorHandle.AddrOfPinnedObject(), errorBuffer.Length); if (xpsNativePtr == IntPtr.Zero) { var nativeError = Encoding.UTF8.GetString(errorBuffer).TrimEnd((char)0); throw new Exception(nativeError); } if (pageCount == 0) { throw new Exception("Initialization failed"); } _nativeXps = new NativeXps(xpsNativePtr, pageCount, _dataHandle); } } catch (RenderException) { _dataHandle.Free(); _nativeXps = null; throw; } catch (Exception e) { _dataHandle.Free(); _nativeXps = null; throw new RenderException("Initialization failed"); } finally { errorHandle.Free(); } }