vb.net - Alternate ways to cancel async task -


i trying find best way , none of attempts far has worked. looking additional input.

i have sub needs executed in background , this

sub start_work()   'step 1  sleep(1000)  'step 2   sleep(2000)  'step3   sleep(1000)  'step 4  end sub 

i call so

dim task1 task task1 = task.factory.startnew(sub() start_test()) 

all examples of cancelling task have seen use loop continuously check cancellation token little hard in scenario.

i thinking of adding while loop , states unsure how have code wait second or 2 before going next step.

any comments/suggestions welcome.

you use manualresetevent handle

the manualresetevent allow stop right away , not wait full sleep time

in example never reach console.writeline("start wait 3")

imports system.threading imports system.threading.tasks  module module1  dim mrewait new manualresetevent(false) dim stoptask boolean  sub main()     dim task1 task     task1 = task.factory.startnew(sub() start_work())     console.writeline("waiting")     thread.sleep(1500)     console.writeline("stop")     stoptask = true     mrewait.set()     console.read() end sub  sub start_work()     mrewait.reset()     console.writeline("start wait 1")     mrewait.waitone(1000)     if stoptask return      console.writeline("start wait 2")     mrewait.waitone(1000)     if stoptask return      console.writeline("start wait 3")     mrewait.waitone(1000)     if stoptask return      console.writeline("stop start_work") end sub  end module