Ejemplo n.º 1
0
        /// <summary>
        /// Runs the specified G'MIC command.
        /// </summary>
        /// <param name="command">The G'MIC command.</param>
        /// <param name="customResourcePath">The custom resource path.</param>
        /// <param name="hostName">The host application name.</param>
        /// <param name="imageList">The image list.</param>
        /// <param name="hasProgressEvent"><c>true</c> if the caller whats progress reports; otherwise, <c>false</c>.</param>
        /// <param name="taskState">The task state.</param>
        /// <exception cref="InvalidOperationException">This G'MIC instance is already running.</exception>
        /// <exception cref="OperationCanceledException">The operation was canceled.</exception>
        public void StartAsync(string command,
                               string customResourcePath,
                               string hostName,
                               GmicImageList imageList,
                               bool hasProgressEvent,
                               GmicRunnerTaskState <TGmicBitmap> taskState)
        {
            if (IsBusy)
            {
                ExceptionUtil.ThrowInvalidOperationException("This G'MIC instance is already running.");
            }

            progress    = -1;
            shouldAbort = 0;

            GmicWorkerArgs args = new GmicWorkerArgs(command,
                                                     customResourcePath,
                                                     hostName,
                                                     imageList,
                                                     hasProgressEvent,
                                                     taskState);

            Task task = Task.Run(() => GmicWorker(args), taskState?.CancellationToken ?? CancellationToken.None);

            IsBusy = TaskIsRunning(task);
        }
Ejemplo n.º 2
0
        private unsafe void GmicWorker(GmicWorkerArgs args)
        {
            Exception error    = null;
            bool      canceled = false;

            CancellationTokenRegistration cancellationTokenRegistration = new CancellationTokenRegistration();

            try
            {
                if (args.Task != null)
                {
                    if (args.CanBeCanceled)
                    {
                        if (args.CancellationToken.IsCancellationRequested)
                        {
                            canceled = true;
                        }
                        else
                        {
                            cancellationTokenRegistration = args.CancellationToken.Register(SignalCancelRequest);
                        }
                    }
                }
                else
                {
                    canceled = shouldAbort != 0;
                }

                if (!canceled)
                {
                    GmicOptions options = new GmicOptions(args.Command,
                                                          args.CustomResourcePath,
                                                          args.HostName);

                    if (args.HasProgressEvent)
                    {
                        if (args.CanBeCanceled)
                        {
                            fixed(float *pProgress = &progress)
                            fixed(byte *pShouldAbort = &shouldAbort)
                            {
                                options.progress = pProgress;
                                options.abort    = pShouldAbort;

                                GmicNative.RunGmic(args.ImageList.SafeImageListHandle, options);
                            }

                            canceled = shouldAbort != 0;
                        }
                        else
                        {
                            fixed(float *pProgress = &progress)
                            {
                                options.progress = pProgress;

                                GmicNative.RunGmic(args.ImageList.SafeImageListHandle, options);
                            }
                        }
                    }
                    else if (args.CanBeCanceled)
                    {
                        fixed(byte *pShouldAbort = &shouldAbort)
                        {
                            options.abort = pShouldAbort;

                            GmicNative.RunGmic(args.ImageList.SafeImageListHandle, options);
                        }

                        canceled = shouldAbort != 0;
                    }
                    else
                    {
                        GmicNative.RunGmic(args.ImageList.SafeImageListHandle, options);
                    }
                }
            }
            catch (Exception ex)
            {
                error = ex;
            }
            finally
            {
                cancellationTokenRegistration.Dispose();
            }

            GmicWorkerCompleted(error, canceled, args.Task);
        }