Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. Problems with thread cancellation.

Problems with thread cancellation.

Scheduled Pinned Locked Moved C#
helpquestion
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    Chen Pang
    wrote on last edited by
    #1

    I used the "Abort()" method of class Thread in a program to cancel a thread. It seemed to function correctly, but after the main program exited, the process can still be found in processes tab of "Windows Task Manager". Whenever this line is not executed, the problem does not occur. Is it inappropriate to cancel a thread in this manner?

    C 1 Reply Last reply
    0
    • C Chen Pang

      I used the "Abort()" method of class Thread in a program to cancel a thread. It seemed to function correctly, but after the main program exited, the process can still be found in processes tab of "Windows Task Manager". Whenever this line is not executed, the problem does not occur. Is it inappropriate to cancel a thread in this manner?

      C Offline
      C Offline
      Corinna John
      wrote on last edited by
      #2
      1. Do you catch the ThreadAbortException in a loop like that? try{ for(...){ try{ }catch{ ... } } }catch(ThreadAbortException){ ... } Then the thread can run for hours without doing anything. 2) You can stop the thread a clean way. Declare a boolean variable, and check it's value in the thread method: bool threadStopped = false; ... private void myThreadMethod(){ doSomething(); if(stopped){ return; } doSomethingElse(); if(stopped){ return; } doAnything(); if(stopped){ return; } ... } With that stop-variable you don't need Thread.Abort() and the try/catch stuff, because the thread stops when you set threadStopped = true.
      C 1 Reply Last reply
      0
      • C Corinna John
        1. Do you catch the ThreadAbortException in a loop like that? try{ for(...){ try{ }catch{ ... } } }catch(ThreadAbortException){ ... } Then the thread can run for hours without doing anything. 2) You can stop the thread a clean way. Declare a boolean variable, and check it's value in the thread method: bool threadStopped = false; ... private void myThreadMethod(){ doSomething(); if(stopped){ return; } doSomethingElse(); if(stopped){ return; } doAnything(); if(stopped){ return; } ... } With that stop-variable you don't need Thread.Abort() and the try/catch stuff, because the thread stops when you set threadStopped = true.
        C Offline
        C Offline
        Chen Pang
        wrote on last edited by
        #3

        I did not catch the ThreadAbortException in a loop and the execution of the tread is actually stoped. There's still one problem with your codes: what if I want the tread to stop immediately when it is "doing something"?

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups