Are there any ways to make the PASSING of tasks (not completion) caused from another task?

for (int i = 0; i < count; i++) { await Task.Run(() => FullReg()); await Task.Delay(5000); if (checkBox1.Checked) { if (i % proxyswitch == 0 && i != 0) { //Task.WaitAll(); -- ТУТ ДОЛЖНА БЫТЬ ПРИОСТАНОВКА ВСЕХ ТАСКОВ System.Diagnostics.Process.Start(@"C:\Program Files (x86)\HMA! Pro VPN\bin\HMA! Pro VPN.exe", "-changeip"); await Task.Delay(10000); } } } while(int.Parse(CountFailedAccLabel.Text) + int.Parse(CountFailedAccLabel.Text) != count) { await Task.Delay(proxyswitch*5000); await Task.Run(() => ChangeIPHMAButton_Click(sender, e)); await Task.Delay(10000); //System.Diagnostics.Process.Start(@"C:\Program Files (x86)\HMA! Pro VPN\bin\HMA! Pro VPN.exe", "-changeip"); } 

    3 answers 3

    You cannot stop another code just like that. This code should cooperate. For example, you can cock a boolean flag, which should be checked in parallel with the running code, or lock a common object that your parallel code should from time to time try to block from releasing.

    • one
      By the way, there is a lot of Task.Delay in your code, which probably means architectural problems. - VladD

    Already found himself done through lock

     private object syncObj = new object(); public async void TestProxy() { for (int i = 5; i <= 5; i++) { Console.WriteLine(i + " task started!"); TaskRegSimsms(i); await Task.Delay(3000); if (i == 5) { lock (syncObj) { Console.WriteLine("LOCKED!!!"); Thread.Sleep(10000); } } } } public async void TaskRegSimsms(int x) { lock (syncObj) { } Console.WriteLine("Count: " + 1 + " of " + x + " task"); await Task.Delay(1000); Console.WriteLine("Count: " + 2 + " of " + x + " task"); await Task.Delay(1000); Console.WriteLine("Count: " + 3 + " of " + x + " task"); await Task.Delay(1000); lock (syncObj) { } Console.WriteLine("Count: " + 4 + " of " + x + " task"); await Task.Delay(1000); lock (syncObj) { } Console.WriteLine("Count: " + 5 + " of " + x + " task"); await Task.Delay(1000); Console.WriteLine("Count: " + 6 + " of " + x + " task"); await Task.Delay(1000); Console.WriteLine("Count: " + 7 + " of " + x + " task"); await Task.Delay(1000); Console.WriteLine("Count: " + 8 + " of " + x + " task"); await Task.Delay(1000); Console.WriteLine("Count: " + 9 + " of " + x + " task"); await Task.Delay(1000); Console.WriteLine("Count: " + 10 + " of " + x + " task"); await Task.Delay(1000); } 

      Possibly task.Join(); will help.

      • Try to write more detailed answers. Explain what is the basis of your statement? - Nicolas Chabanovsky