Tasks and fo-dicom

1.1k Views Asked by At

EDIT: Problem has shifted to ending before all events are finished.


I'm having a problem figuring out Tasks with fo-dicom library.

The request is sendasync but the response is inside of a event delegate.

My sample code sends a request (Find Study), gets a response and spawns another request (Find Series for above study) that also has a response. Eventually I need to then send a Move request, and then queue up a verify task that is delayed.

I'm sure I have a just an issue with my Tasks/Async logic.

Sample code

List of things to do but Task.WhenAll(t) ends before all the tasks object events are finished.

How do I hold a task until an event is fired?

       List<Task<DicomMoveResult>> t = new List<Task<DicomMoveResult>>();
         foreach (var item in dbSet)
         {
            t.Add(DicomMove(item));
         }
         var x = await Task.WhenAll(t);
         Console.WriteLine("Finished");

Work Method

      private static async Task<DicomMoveResult> DicomMove(DicomMoveRequest request)
            {
                DicomMoveResult r = new DicomMoveResult() { Request = request };

                var client = new DicomClient();

                var cfindStudy = DicomCFindRequest.CreateStudyQuery(accession: request.Accession);

                cfindStudy.OnResponseReceived += async (DicomCFindRequest rq, DicomCFindResponse rp) =>
                {

                    if (rp.Status.State == DicomState.Pending)
                    {
                        if (rp.HasDataset)
                        {
                            var studyInstanceUID = rp.Dataset.Get<string>(DicomTag.StudyInstanceUID);
                            r.StudyId = studyInstanceUID;

                            var cfindSeries = DicomCFindRequest.CreateSeriesQuery(studyInstanceUID);
                            cfindSeries.OnResponseReceived += (DicomCFindRequest srq, DicomCFindResponse srp) =>
                            {
                                if (srp.Status.State == DicomState.Pending)
                                {
                                    if (srp.HasDataset)
                                    {
                                        var seriesInstanceUID = srp.Dataset.Get<string>(DicomTag.SeriesInstanceUID);
                                        r.SeriesId.Add(seriesInstanceUID);

                                    }
                                }


                            };

                            client.AddRequest(cfindSeries);
                            await client.SendAsync(request.PACSIP, request.PACSPort, false, request.CallingAE, request.PACSAE);

                        }

                    }


                };

                client.AddRequest(cfindStudy);
                await client.SendAsync(request.PACSIP, request.PACSPort, false, request.CallingAE, request.PACSAE);

                return r;
            }
0

There are 0 best solutions below