0

I have an async operation that takes in an List of classes available in Train and get the fare based on the classes asynchronously and returns some data. I am trying to get all those fares within predefined time. So, here I want to cancel the tasks which are not completed within the predefined time.

    public async Task<string> AvailabilityEnquiry(AvailabilityEnquiry LobjAvailabilityEnquiryRequest)
{

List<Task<string>> taskList = new List<Task<string>>();

foreach (var journey in ClassesToGetFares)
{
    taskList.Add(Task.Run(() => IRCTC_GetFare()));
}

await Task.WhenAny(Task.WhenAll(taskList), Task.Delay(TimeSpan.FromSeconds(Convert.ToDouble(ConfigurationManager.AppSettings["TimeOutInSeconds"]))));
                    var completedResults = taskList
                                        .Where(t => t.Status == TaskStatus.RanToCompletion)
                                        .Select(t => t.Result)
                                        .ToList();

}

public static async  Task<string> IRCTC_GetFare()
{
    
 LstrApiData = await Lobjrestclient.makeRequest_POSTAsync("IRCTC", LstrJsonData);
 return "Success";
}
  • 2
    You should make your questions (and your lines) a lot shorter. – H H Aug 08 '21 at 16:28
  • Does the `IRCTC_GetFare` method accept a `CancellationToken` parameter? – Theodor Zoulias Aug 08 '21 at 16:35
  • 1
    And when this is an asp.net app then you should remove Task.Run() and make _GetFare() async (with cancelToken). – H H Aug 08 '21 at 16:44
  • 2
    Related questions: [How do I abort/cancel TPL Tasks?](https://stackoverflow.com/questions/4783865/how-do-i-abort-cancel-tpl-tasks), [How to cancel a Task in await?](https://stackoverflow.com/questions/10134310/how-to-cancel-a-task-in-await) – Theodor Zoulias Aug 08 '21 at 16:46
  • @ Theodor Zoulias , IRCTC_GetFare method doesnot accept cancellation token – VIGNESH WARAN Aug 09 '21 at 08:00
  • 1
    @VIGNESHWARAN Then there is no way to safely cancel these tasks. – Lasse V. Karlsen Aug 09 '21 at 08:09
  • 1
    I know it's not your question, but `List, string, bool>>>` is way too much. Not only is it too much for a minimal example for an SO question, you never want to have such a deep nesting in generics. If you need a tuple of a list, a string and a bool, than you don't need a tuple - you need a class/struct/record. – asaf92 Aug 09 '21 at 08:46
  • @Lasse V. Karlsen, Any alteration in code ,could make it cancel safely ? – VIGNESH WARAN Aug 09 '21 at 08:57
  • @ asaf92, Thank you for your suggestion, will make it class instead of tuple. – VIGNESH WARAN Aug 09 '21 at 09:35
  • @Henk Holterman , Could not understand clearly.Can you clarify a bit more ? – VIGNESH WARAN Aug 09 '21 at 09:36
  • Add a tag to the specific framework. If it's .net 5.0 then you can [use this simple feature](https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/concepts/async/cancel-async-tasks-after-a-period-of-time). But first and foremost check if the `IRCTC_GetFare` can be made `async` (and accept cancellation token), if not then check if it's thread safe at all and if yes, check if there's any benefit to it running in parallel because it may have its own locks, defeating the purpose of your code. – bokibeg Aug 09 '21 at 10:17
  • @bokibeg...Thanks for your suggestion....net 4.5.2 framework.In Irctc_getfare, calling Api to get the fares.if it is 10 trains,each train has 4 classes.So i have to call api 40 times to get the fare which would be in the way of asynchronous.Can you help me to alter this code? – VIGNESH WARAN Aug 09 '21 at 17:29

0 Answers0