private static void HandleCancellation(CreateCollageAsyncContext context, ref bool isCancelled) { if (context != null) { if (context.IsCancelling) { isCancelled = true; } } }
public void CreateAsync() { var worker = new CreateTaskWorkerDelegate(CreateCollage); var completedCallback = new AsyncCallback(CreateTaskCompletedCallback); lock (this.sync) { if (this.isBusy) { ThrowExceptions.Custom(Exc.GetStackTrace(), type, Exc.CallingMethod(), "The engine is currently busy."); } AsyncOperation async = AsyncOperationManager.CreateOperation(null); var context = new CreateCollageAsyncContext(); bool cancelled; worker.BeginInvoke(async, context, out cancelled, completedCallback, async); this.isBusy = true; this.createTaskContext = context; } }
private void CreateCollage(AsyncOperation async, CreateCollageAsyncContext context, out bool isCancelled) { isCancelled = false; using (var bitmapCollage = new Bitmap(this.settings.Dimensions.TotalWidth, this.settings.Dimensions.TotalHeight)) { using (Graphics graphics = bitmapCollage.CreateGraphics()) { for (int rowsCounter = 0; rowsCounter < this.settings.Dimensions.NumberOfRows; rowsCounter++) { for (int colsCounter = 0; colsCounter < this.settings.Dimensions.NumberOfColumns; colsCounter++) { this.ReportProgress(async, colsCounter, rowsCounter); HandleCancellation(context, ref isCancelled); DrawTile(graphics, colsCounter, rowsCounter); } } } this.collageSaver.Save(bitmapCollage); } }
public void CreateAsync() { var worker = new CreateTaskWorkerDelegate(CreateCollage); var completedCallback = new AsyncCallback(CreateTaskCompletedCallback); lock (this.sync) { if (this.isBusy) { throw new InvalidOperationException("The engine is currently busy."); } AsyncOperation async = AsyncOperationManager.CreateOperation(null); var context = new CreateCollageAsyncContext(); bool cancelled; worker.BeginInvoke(async, context, out cancelled, completedCallback, async); this.isBusy = true; this.createTaskContext = context; } }