private static AddDicomWorkerResultState GetCancelResult(int totalCount, int successCount, int failureCount)
        {
            AddDicomWorkerResultState resultState = new AddDicomWorkerResultState();

            resultState.TotalCount   = totalCount;
            resultState.SuccessCount = successCount;
            resultState.FailedCount  = failureCount;
            resultState.Cancelled    = true;
            return(resultState);
        }
        protected override void OnDoWork(DoWorkEventArgs e)
        {
            List <string> dicomFiles;
            int           count             = 0;
            int           successCount      = 0;
            int           failureCount      = 0;
            int           selectedFileCount = 0;
            int           totalFileCount    = 0;

            string[] selectedFileArray = e.Argument as string[];
            if (selectedFileArray != null)
            {
                selectedFileCount = selectedFileArray.Length;
            }

            if (this.CancellationPending)
            {
                // e.Cancel = true;
                e.Result = GetCancelResult(selectedFileCount, successCount, failureCount);
                return;
            }


            dicomFiles = GetDicomFiles(e);

            if (dicomFiles != null)
            {
                totalFileCount = dicomFiles.Count;
            }

            if (this.CancellationPending)
            {
                // e.Cancel = true;
                e.Result = GetCancelResult(totalFileCount, successCount, failureCount);
                return;
            }

            MultiDicomImportEventArgs args = new MultiDicomImportEventArgs(totalFileCount);

            EventBroker.Instance.PublishEvent <MultiDicomImportEventArgs>(this, args);


            foreach (string file in dicomFiles)
            {
                if (this.CancellationPending)
                {
                    // e.Cancel = true;
                    e.Result = GetCancelResult(totalFileCount, successCount, failureCount);
                    return;
                }

                using (DicomDataSet ds = new DicomDataSet( ))
                {
                    DicomCommandStatusType status;
                    string message        = string.Empty;
                    bool   successfulLoad = true;

                    try
                    {
                        ds.Load(file, DicomDataSetLoadFlags.None);
                    }
                    catch (Exception ex)
                    {
                        successfulLoad = false;
                        message        = ex.Message;
                        if (!File.Exists(file))
                        {
                            message = string.Format(@"File does not exist");

                            // This is needed for the following case
                            // If importing a DICOMDIR references a large number of files that do not exist, processing occurs so quickly that the import cannot be cancelled.
                            // So in the case that a file does not exists (i.e. abnormal condition), sleep for 100 ms to give the user time to cancel
                            Thread.Sleep(100);
                        }
                    }

                    count++;

                    AddDicomWorkerProgressState state;
                    StoreClientSessionProxy     proxy;
                    InstanceCStoreCommand       cmd;

                    if (successfulLoad && !OnCancelStore(ds, out message))
                    {
                        proxy = new StoreClientSessionProxy();
                        cmd   = new InstanceCStoreCommand(proxy, ds, _DataAccess);


                        proxy.AffectedSOPInstance = ds.GetValue <string>(DicomTag.SOPInstanceUID, string.Empty);
                        proxy.AbstractClass       = ds.GetValue <string>(DicomTag.SOPClassUID, string.Empty);
                        proxy.ServerName          = _AETitle;
                        proxy.ClientName          = _AETitle;


                        ds.InsertElementAndSetValue(DicomTag.MediaStorageSOPInstanceUID, proxy.AffectedSOPInstance);
                        ds.InsertElementAndSetValue(DicomTag.ImplementationClassUID, _ImplementationClassUID);

                        state = new AddDicomWorkerProgressState();

                        OnStoreCommandCreated(this, new StoreCommandEventArgs(cmd));

                        cmd.Execute();

                        status  = proxy.LastStatus;
                        message = proxy.LastStatusDescriptionMessage;
                    }
                    else
                    {
                        state  = new AddDicomWorkerProgressState();
                        status = DicomCommandStatusType.ProcessingFailure;
                    }

                    if (status == DicomCommandStatusType.Success)
                    {
                        successCount++;

                        state.CurrentCount = successCount;
                    }
                    else
                    {
                        failureCount++;

                        state.CurrentCount = failureCount;
                    }

                    state.Status        = status;
                    state.TotalCount    = dicomFiles.Count;
                    state.File          = file;
                    state.LoadedDataSet = ds;
                    state.Description   = message;

                    ReportProgress((count * 100) / dicomFiles.Count, state);
                }
            }

            AddDicomWorkerResultState resultState = new AddDicomWorkerResultState();

            resultState.SuccessCount = successCount;
            resultState.FailedCount  = failureCount;
            e.Result = resultState;
        }