void Batch <T>(T[] items,
                       int passes,
                       Action onBegin,
                       Action <Exception> onEnd,
                       Action <int> onBeginPass,
                       Action <int> onEndPass,
                       Action <T, int> onItem,
                       DeliveryImportManagerState activeState)
        {
            ThrowIfNotIdle();
            this.State = activeState;

            onBegin();
            Exception exception = null;

            try
            {
                for (int pass = 0; pass < passes; pass++)
                {
                    onBeginPass(pass);
                    for (int i = 0; i < items.Length; i++)
                    {
                        onItem(items[i], pass);
                    }
                    onEndPass(pass);
                }
            }
            catch (DeliveryConflictException dceex)
            {
                throw dceex;
            }
            catch (Exception ex)
            {
                exception = ex;
            }

            try
            {
                onEnd(exception);
            }
            catch (Exception ex)
            {
                if (exception == null)
                {
                    exception = ex;
                }
                else
                {
                    Log.Write("Failed to end delivery operation - probably because of another exception. See next log message.", ex);
                }
            }
            finally
            {
                this.State = DeliveryImportManagerState.Idle;
            }


            // Throw exception if found
            if (exception != null)
            {
                throw new Exception("Delivery operation failed while importing.", exception);
            }
        }
        void Batch(Delivery[] deliveries,
                   int passes,
                   Action onBegin,
                   Action <Exception> onEnd,
                   Action <int> onBeginPass,
                   Action <int> onEndPass,
                   Action <int> onItem,
                   Action onDispose,
                   DeliveryImportManagerState activeState,
                   DeliveryOperation historyOperation)
        {
            ThrowIfNotIdle();
            this.State = activeState;
            Dictionary <string, object>[] entryParams = new Dictionary <string, object> [deliveries.Length];

            onBegin();
            Exception exception = null;

            try
            {
                for (int pass = 0; pass < passes; pass++)
                {
                    onBeginPass(pass);
                    for (int i = 0; i < deliveries.Length; i++)
                    {
                        this.CurrentDelivery        = deliveries[i];
                        this.HistoryEntryParameters = entryParams[i] ?? (entryParams[i] = new Dictionary <string, object>());

                        onItem(pass);
                    }
                    onEndPass(pass);
                }
            }
            catch (Exception ex)
            {
                exception = ex;
            }

            this.CurrentDelivery        = null;
            this.HistoryEntryParameters = null;

            onEnd(exception);

            if (exception == null)
            {
                // Add history and save
                for (int i = 0; i < deliveries.Length; i++)
                {
                    Delivery delivery = deliveries[i];
                    delivery.History.Add(historyOperation, this._serviceInstanceID, entryParams[i]);
                    delivery.Save();
                }
            }
            else
            {
                // Throw exception
                throw new Exception("InnerException:", exception);
            }

            this.State = DeliveryImportManagerState.Idle;

            onDispose();
            OnDispose();
        }
        void Batch(Delivery[] deliveries,
                   int passes,
                   Action onBegin,
                   Action <Exception> onEnd,
                   Action <int> onBeginPass,
                   Action <int> onEndPass,
                   Action <int> onItem,
                   Action onDispose,
                   DeliveryImportManagerState activeState,
                   DeliveryOperation historyOperation)
        {
            ThrowIfNotIdle();
            this.State = activeState;
            Dictionary <string, object>[] entryParams = new Dictionary <string, object> [deliveries.Length];

            onBegin();
            Exception exception = null;

            try
            {
                for (int pass = 0; pass < passes; pass++)
                {
                    onBeginPass(pass);
                    for (int i = 0; i < deliveries.Length; i++)
                    {
                        this.CurrentDelivery        = deliveries[i];
                        this.HistoryEntryParameters = entryParams[i] ?? (entryParams[i] = new Dictionary <string, object>());

                        onItem(pass);
                    }
                    onEndPass(pass);
                }
            }
            catch (DeliveryConflictException dceex)
            {
                throw dceex;
            }
            catch (Exception ex)
            {
                exception = ex;
            }

            this.CurrentDelivery        = null;
            this.HistoryEntryParameters = null;

            try
            {
                onEnd(exception);
            }
            catch (Exception ex)
            {
                if (exception == null)
                {
                    throw new Exception("Failed to end delivery operation. There were no other exceptions before this one.", ex);
                }
                else
                {
                    Log.Write("Failed to end delivery operation - probably because of another exception. See next log message.", ex);
                }
            }

            if (exception == null)
            {
                // Add history and save
                for (int i = 0; i < deliveries.Length; i++)
                {
                    Delivery delivery = deliveries[i];
                    delivery.History.Add(historyOperation, this._serviceInstanceID, entryParams[i]);
                    delivery.Save();
                }
            }
            else
            {
                // Throw exception
                throw new Exception("Delivery operation failed while importing.", exception);
            }

            this.State = DeliveryImportManagerState.Idle;

            onDispose();
            OnDispose();
        }