using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
+using System.Threading;
using System.Threading.Tasks;
using static Interop;
using static Interop.Decode;
}
}
- internal Task<IEnumerable<BitmapFrame>> DecodeAsync()
+ private IEnumerable<BitmapFrame> RunDecoding()
{
- Initialize(Handle);
-
IntPtr outBuffer = IntPtr.Zero;
- SetOutputBuffer(Handle, out outBuffer).ThrowIfFailed("Failed to decode given image");
- var tcs = new TaskCompletionSource<IEnumerable<BitmapFrame>>();
-
- Task.Run(() =>
+ try
{
- try
- {
- int width, height;
- ulong size;
+ SetOutputBuffer(Handle, out outBuffer).ThrowIfFailed("Failed to decode given image");
- DecodeRun(Handle, out width, out height, out size).ThrowIfFailed("Failed to decode");
+ DecodeRun(Handle, out var width, out var height, out var size).
+ ThrowIfFailed("Failed to decode");
- tcs.SetResult(new[] { new BitmapFrame(outBuffer, width, height, (int)size) });
- }
- catch (Exception e)
- {
- tcs.TrySetException(e);
- }
- finally
- {
- LibcSupport.Free(outBuffer);
- }
- });
+ yield return new BitmapFrame(outBuffer, width, height, (int)size);
+ }
+ finally
+ {
+ LibcSupport.Free(outBuffer);
+ }
+ }
+
+ internal Task<IEnumerable<BitmapFrame>> DecodeAsync()
+ {
+ Initialize(Handle);
- return tcs.Task;
+ return Task.Factory.StartNew(RunDecoding, CancellationToken.None,
+ TaskCreationOptions.DenyChildAttach | TaskCreationOptions.LongRunning,
+ TaskScheduler.Default);
}
internal virtual void Initialize(ImageDecoderHandle handle)
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
+using System.Threading;
using System.Threading.Tasks;
using static Interop.ImageUtil;
using Unmanaged = Interop.ImageUtil.Encode;
ThrowIfFailed("Failed to set the color space");
}
- private Task Run(Stream outStream)
+ private void RunEncoding(object outStream)
{
- var tcs = new TaskCompletionSource<bool>();
+ IntPtr outBuffer = IntPtr.Zero;
- Task.Run(() =>
+ try
{
- IntPtr outBuffer = IntPtr.Zero;
-
- try
- {
- Unmanaged.SetOutputBuffer(Handle, out outBuffer).ThrowIfFailed("Failed to initialize encoder");
-
- ulong size = 0;
- Unmanaged.Run(Handle, out size).ThrowIfFailed("Failed to encode given image");
+ Unmanaged.SetOutputBuffer(Handle, out outBuffer).ThrowIfFailed("Failed to initialize encoder");
- byte[] buf = new byte[size];
- Marshal.Copy(outBuffer, buf, 0, (int)size);
- outStream.Write(buf, 0, (int)size);
+ Unmanaged.Run(Handle, out var size).ThrowIfFailed("Failed to encode given image");
- tcs.TrySetResult(true);
- }
- catch (Exception e)
- {
- tcs.TrySetException(e);
- }
- finally
- {
- Interop.Libc.Free(outBuffer);
- }
- });
+ byte[] buf = new byte[size];
+ Marshal.Copy(outBuffer, buf, 0, (int)size);
+ (outStream as Stream).Write(buf, 0, (int)size);
+ }
+ finally
+ {
+ Interop.Libc.Free(outBuffer);
+ }
+ }
- return tcs.Task;
+ private Task Run(Stream outStream)
+ {
+ return Task.Factory.StartNew(RunEncoding, outStream, CancellationToken.None,
+ TaskCreationOptions.DenyChildAttach | TaskCreationOptions.LongRunning,
+ TaskScheduler.Default);
}
internal Task EncodeAsync(Action<ImageEncoderHandle> settingInputAction, Stream outStream)