Hi , I need some help in below scenerio.
I have 2 task which are running parallel where some operations are going on where 1st Task is completing in 5 min and in 2nd monthd its taking to complete around hrs.
Task.Run ( async()=> { await oneMethod()}) ;
Task.Run ( async()=> { await SecondMethod()}) ;
Now, I just wanna to execute first Task.Run when 2nd task is taking more than 15 min.
how to do that

Mohammad HussainRank #513 · 4.8K reputationPosted Aug 7, 2023, 10:56 AM EDT
using System; using System.Threading.Tasks; class Program { static async Task Main() { Task task1 = Task.Run(async () => { await OneMethod(); }); Task task2 = Task.Run(async () => { await SecondMethod(); }); // Set a timer for 15 minutes var timer = new System.Timers.Timer(15 * 60 * 1000); // 15 minutes timer.Elapsed += (sender, e) => { // If the 2nd task is still running, cancel it and start the 1st task if (!task2.IsCompleted) { task2.ContinueWith(t => Console.WriteLine("Second task canceled due to timeout.")); task2 = task1; } // Stop the timer timer.Stop(); }; timer.Start(); // Wait for both tasks to complete await Task.WhenAll(task1, task2); Console.WriteLine("Both tasks completed."); } static async Task OneMethod() { // Some time-consuming operation for the first task await Task.Delay(5 * 60 * 1000); // 5 minutes Console.WriteLine("First task completed."); } static async Task SecondMethod() { // Some time-consuming operation for the second task await Task.Delay(2 * 60 * 60 * 1000); // 2 hours Console.WriteLine("Second task completed."); } }Udit ChauhanRank #3,035 · 475 reputationPosted Aug 8, 2023, 4:25 AM EDT
Here , you cancel the 2nd task . But we dont need to cancel the 2nd task , it will go as usual and start the 1st task again.
How to do that