Example #1
0
        /// <summary>
        /// Convenience method for creating a C-FIND image query.
        /// </summary>
        /// <param name="studyInstanceUid">Study instance UID.</param>
        /// <param name="seriesInstanceUid">Series instance UID.</param>
        /// <param name="modality">Modality.</param>
        /// <returns>C-FIND image query object.</returns>
        public static DicomCFindRequest CreateImageQuery(
            string studyInstanceUid,
            string seriesInstanceUid,
            string modality = null)
        {
            DicomCFindRequest dimse = DicomCFindRequest.CreateImageQuery(studyInstanceUid, seriesInstanceUid, modality);

            return(dimse);
        }
        private async Task <IEnumerable <ImageDesc> > GetImagesDescAsync(Series series, DicomSearchServiceSettings settings, CancellationToken ct)
        {
            var uid = series.SeriesInstanceUid;
            var max = settings.SeriesImageSettings.SeriesImageTakeImages;

            var result = new List <ImageDesc>();

            if (ct.IsCancellationRequested)
            {
                return(result);
            }

            var request = DicomCFindRequest.CreateImageQuery(series.StudyInstanceUid, series.SeriesInstanceUid);

            CancellationTokenSource tooManySeriesCts = null;
            var cancellationLock = new object();

            request.OnResponseReceived += (req, resp) =>
            {
                if (ct.IsCancellationRequested || resp.Dataset == null)
                {
                    return;
                }

                if (result.Count >= settings.SeriesImageSettings.SeriesImageTakeImages)
                {
                    lock (cancellationLock)
                    {
                        if (!tooManySeriesCts.IsCancellationRequested)
                        {
                            tooManySeriesCts.Cancel();
                        }
                    }

                    if (tooManySeriesCts.IsCancellationRequested)
                    {
                        _logger.Debug("Cancelled Image query for series {Uid} after {Max}", uid, max);
                    }

                    return;
                }

                var imageDesc = resp.Dataset.CreateObject <ImageDesc>();
                if (imageDesc == null)
                {
                    _logger.Error("ImageDesc creation failed");
                    return;
                }

                lock (cancellationLock)
                {
                    result.Add(imageDesc);
                }

                _logger.Debug("Got imageDesc for series {Uid} on image {ImageUid}. Results: {Count}", uid, imageDesc.SopInstanceUid, result.Count);
            };

            using (tooManySeriesCts = new CancellationTokenSource())
            {
                using (CancellationTokenSource linkedCts = CancellationTokenSource.CreateLinkedTokenSource(ct, tooManySeriesCts.Token))
                {
                    _logger.Debug("Getting images desc for series {SeriesInstanceUid}", series.SeriesInstanceUid);

                    await DicomSearchService.FindAsync <ImageDesc>(request, settings, null, linkedCts.Token);

                    return(result);
                }
            }
        }