none
How to stop the task? RRS feed

  • Question

  • Hi, I have a 3 separate tasks: 1-st and 2-nd are doing some heavy calculation. During this process, my 3-rd task is round wheel on the screen.

    Dim Task1 As New Task(Sub() Load())
    Task1.Start()
    
    Dim Task2 As New Task(Sub() Unload())
    Task2.Start()
    
    Dim Task3 As New Task(Sub() RoundWheel())
    Task3.Start()
    
    Task.WaitAll(Task1, Task2)
    
    Task3...

     As you can see here is Task1, Task2 and Task3. I'm waiting till Task1 and Task2 will be ended and terminated naturally. When it's done, I need to stop permanently rounding wheel task. But I see no Stop! In Internet some examples showing Cancellation process in a farm by token with implementation delegate and extra class to catch cancellation request from the task. But I don't need to catch it from the task in a farm. I need to stop it as soon as I need from outside of this task. How this can be done? Thanks. 


    Aleksey


    • Edited by Newfriend Monday, August 19, 2013 8:29 AM
    Monday, August 19, 2013 8:27 AM

Answers

  •     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim Task1 As New Task(Sub() Task1_Work())
            Task1.Start()
    
            Dim Task2 As New Task(Sub() Task2_Work())
            Task2.Start()
    
            Dim tokenSource As New CancellationTokenSource()
            Dim ct As CancellationToken = tokenSource.Token
    
            Dim Task3 As New Task(Sub() Task3_Work(ct), ct)
            Task3.Start()
    
            Task.WaitAll(Task1, Task2)
    
            tokenSource.Cancel() '<-- request to stop spinning the wheel
    
        End Sub
    
        Private Sub Task1_Work()
            Threading.Thread.Sleep(2000)
        End Sub
    
        Private Sub Task2_Work()
            Threading.Thread.Sleep(6000)
        End Sub
    
        Private Sub Task3_Work(ByVal ct As Threading.CancellationToken)
            Do
                'spin the wheel
                Threading.Thread.Sleep(1)
                If ct.IsCancellationRequested Then Exit Do ' <-- Catch and execute the request to stop spinning the wheel
            Loop
        End Sub
    

    • Marked as answer by Newfriend Tuesday, August 20, 2013 5:28 PM
    Monday, August 19, 2013 10:35 AM

All replies

  • HI Friend,

    Check Below code u will get an idea.

    If it is help full let me know.

    ' How to: Wait on One or More Tasks to Complete 
    Imports System.Threading
    Imports System.Threading.Tasks
    
    Module WaitOnTasks
    
        Dim rand As New Random()
        Sub Main()
    
            ' Wait on a single task with no timeout specified. 
            Dim taskA = Task.Factory.StartNew(Sub() DoSomeWork(10000000))
            taskA.Wait()
            Console.WriteLine("taskA has completed.")
    
    
            ' Wait on a single task with a timeout specified. 
            Dim taskB = Task.Factory.StartNew(Sub() DoSomeWork(10000000))
            taskB.Wait(100) 'Wait for 100 ms.
    
            If (taskB.IsCompleted) Then
                Console.WriteLine("taskB has completed.")
            Else
                Console.WriteLine("Timed out before task2 completed.")
            End If 
    
            ' Wait for all tasks to complete. 
            Dim myTasks(9) As Task
            For i As Integer = 0 To myTasks.Length - 1
                myTasks(i) = Task.Factory.StartNew(Sub() DoSomeWork(10000000))
            Next
            Task.WaitAll(myTasks)
    
            ' Wait for first task to complete. 
            Dim tasks2(2) As Task(Of Double)
    
            ' Try three different approaches to the problem. Take the first one.
            tasks2(0) = Task(Of Double).Factory.StartNew(Function() TrySolution1())
            tasks2(1) = Task(Of Double).Factory.StartNew(Function() TrySolution2())
            tasks2(2) = Task(Of Double).Factory.StartNew(Function() TrySolution3())
    
    
            Dim index As Integer = Task.WaitAny(tasks2)
            Dim d As Double = tasks2(index).Result
            Console.WriteLine("task(0) completed first with result of {1}.", index, d)
            Console.ReadKey()
    
        End Sub 
    
    
        ' Dummy Functions to Simulate Work 
    
        Function DoSomeWork(ByVal val As Integer)
            ' Pretend to do something.
            Thread.SpinWait(val)
        End Function 
    
        Function TrySolution1()
    
            Dim i As Integer = rand.Next(1000000)
            ' Simulate work by spinning
            Thread.SpinWait(i)
            Return i
        End Function 
        Function TrySolution2()
    
            Dim i As Integer = rand.Next(1000000)
            ' Simulate work by spinning
            Thread.SpinWait(i)
            Return i
        End Function 
        Function TrySolution3()
    
            Dim i As Integer = rand.Next(1000000)
            ' Simulate work by spinning
            Thread.SpinWait(i)
            Thread.SpinWait(1000000)
            Return i
        End Function 
    
    End Module

    Monday, August 19, 2013 9:13 AM
  • Hi! Thanks for so informative reply, but I see no solution for my problem. 3 task run simultaneity. I should wait till first two will be finished and then kill 3-rd.

    In your reply I see task by task sequence - I don't need it, all task should be run at once. Then I see an array of task within farm. Here is waitall, which is closer but not the same I need - I need to wait only 1-st and 2-nd. I need to stop 3-rd by myself, because it is permanent loop and there is no end. I see no example how to stop this 3-rd task.


    Aleksey

    Monday, August 19, 2013 9:45 AM
  •     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim Task1 As New Task(Sub() Task1_Work())
            Task1.Start()
    
            Dim Task2 As New Task(Sub() Task2_Work())
            Task2.Start()
    
            Dim tokenSource As New CancellationTokenSource()
            Dim ct As CancellationToken = tokenSource.Token
    
            Dim Task3 As New Task(Sub() Task3_Work(ct), ct)
            Task3.Start()
    
            Task.WaitAll(Task1, Task2)
    
            tokenSource.Cancel() '<-- request to stop spinning the wheel
    
        End Sub
    
        Private Sub Task1_Work()
            Threading.Thread.Sleep(2000)
        End Sub
    
        Private Sub Task2_Work()
            Threading.Thread.Sleep(6000)
        End Sub
    
        Private Sub Task3_Work(ByVal ct As Threading.CancellationToken)
            Do
                'spin the wheel
                Threading.Thread.Sleep(1)
                If ct.IsCancellationRequested Then Exit Do ' <-- Catch and execute the request to stop spinning the wheel
            Loop
        End Sub
    

    • Marked as answer by Newfriend Tuesday, August 20, 2013 5:28 PM
    Monday, August 19, 2013 10:35 AM