public string Execute(string[] args) { using var fileStream = File.OpenRead(Filename); using var image = Image.NewFromStream(fileStream); Parallel.For(0, 1000, new ParallelOptions { MaxDegreeOfParallelism = NetVips.ConcurrencyGet() }, i => RandomCrop(image, TileSize).WriteToFile($"x_{i}.png")); return("Done!"); }
public string Execute(string[] args) { NetVips.CacheSetMax(0); using var fileStream = File.OpenRead(Filename); using var image = Image.NewFromStream(fileStream); for (var i = 0; i < 1000; i++) { using var crop = image.Crop(0, 0, 256, 256); var _ = crop.Avg(); Console.WriteLine($"reference count: {image.RefCount}"); // RefCount should not increase (i.e. operation should be freed) Debug.Assert(image.RefCount == 2u); } var count = 0; var locker = new object(); Parallel.For(0, 1000, new ParallelOptions { MaxDegreeOfParallelism = NetVips.ConcurrencyGet() }, i => { Interlocked.Increment(ref count); using var crop = image.Crop(0, 0, 256, 256); lock (locker) { var _ = crop.Avg(); Console.WriteLine($"reference count: {image.RefCount} with {count} active threads"); // RefCount -1 must be lower than or equal to the number of active threads Debug.Assert(image.RefCount - 1 <= (uint)count); } Interlocked.Decrement(ref count); }); return("All done!"); }