Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8030843
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T00:53:52+00:00 2026-06-05T00:53:52+00:00

I have … Private Sub TestTask() Debug.Write(Running) For i As Integer = 0 To

  • 0

I have …

Private Sub TestTask()
    Debug.Write("Running")

    For i As Integer = 0 To 60
        Debug.Write(".")
        System.Threading.Thread.Sleep(1000)
    Next

    Debug.WriteLine("Finished")
End Sub

….

Dim cts As New CancellationTokenSource
Dim oToken As CancellationToken = cts.Token

'Create HelperTask to wait for cancellation request
Dim oHelperTask As Task = Task.Factory.StartNew(Function()

        'Create Task to invoke function
        Dim oTask As Task = Task.Factory.StartNew(Function()
            Return outerFunction.Invoke
        End Function, oToken)

        ' wait for cancellation token if Task is not complete
        While oTask.Status = TaskStatus.Running
            Thread.Sleep(200)
            If oToken.IsCancellationRequested Then
                oToken.ThrowIfCancellationRequested()
                Return Nothing
            End If
        End While

        Return oTask.Result

End Function, oToken)


cts.cancel()

But in my debug window on visual sudio my TestTask() continues to run with ….. please anyone enlighten me. Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T00:53:53+00:00Added an answer on June 5, 2026 at 12:53 am

    The whole point of the CancellationToken is that the actual worker lambda (or function) should check it to see if it should stop. In your case, TestTask must have access to the token and check it after each iteration. Neither the multiple helper tasks or the checks for the task status or the cancellation request check are necessary.

    The MSDN article on Task Cancelation shows how the only thing required is for the lambda to check the token, nothing more.

    In your case, TestTask can respond to a cancellation with code as simple as this:

    Sub Main()
        Dim cts As New CancellationTokenSource
        Dim token = cts.Token
    
        Task.Factory.StartNew(Sub() TestTask(token), token)
    
        Thread.Sleep(3000)
        cts.Cancel()
        Console.ReadKey()
    End Sub
    
    
    
    Private Sub TestTask(token As CancellationToken)
        Console.Write("Running")
    
        For i As Integer = 0 To 60
    
            token.ThrowIfCancellationRequested()
    
            Console.Write(".")
            Thread.Sleep(1000)
        Next
    
        Console.WriteLine("Finished")
    End Sub
    

    The only thing needed is to pass the token to TestTask and start it like this:

            Task.Factory.StartNew(Sub() TestTask(token), token)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

have next code: class GameTexture { private: LPDIRECT3DTEXTURE9 texture; unsigned char *alphaLayer; UINT width,
I have an element like this: <span class=tool_tip title=The full title>The ful&#8230;</span> This seems
I have this xml <entry id=1008 section=articles> <excerpt><p>&#8230; in Richtung „Aus für Tierversuche. Kosmetik-Fertigprodukte
Have a long running set of discrete tasks: parsing 10s of thousands of lines
Have you used VS.NET Architect Edition's Application and System diagrams to start designing a
have an issue with a fairly basic query I'm attempting to write. select count
Have just started converting an existing job tracking system into an ASP.NET MVC application.
have a php code like this,going to convert it in to C#. function isValid($n){
Have one Doubt In MVC Architecture we can able to pass data from Controller
Have a network location that shows paths in the 8.3 short format. I need

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.