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
  1. Home
  2. General Programming
  3. C#
  4. Thread Abort

Thread Abort

Scheduled Pinned Locked Moved C#
question
4 Posts 3 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.
  • B Offline
    B Offline
    Blue_Skye
    wrote on last edited by
    #1

    Hi! How can we code to make assure that if the thread we want to abort is processing a function, it just be aborted after finish that function ? Thanks!

    L 1 Reply Last reply
    0
    • B Blue_Skye

      Hi! How can we code to make assure that if the thread we want to abort is processing a function, it just be aborted after finish that function ? Thanks!

      L Offline
      L Offline
      LongRange Shooter
      wrote on last edited by
      #2

      Here is one way of stopping your thread, making sure your current execution ends and you do not get an AbortException. You would have something like this to establish the thread in your main processor:

      ThreadClass function = new ThreadClass(); // your process
      myThread = new ThreadStart(function.Process);

      your Process would be something like this:

      public bool ThreadWanted = true;
      ... init/constructor logic ...
      do
      {
      if ( ..no data to process.. )
      Thread.Sleep(50);
      ...function
      }
      while (ThreadWanted)
      return;

      when you want the thread to abort just do this in the thread creator logic:

      public void TerminateProcess()
      {
      function.ThreadWanted = false;
      }

      There are 10 kinds of people in the world.
      Those that read binary...
      ...and those who don't.

      B S 2 Replies Last reply
      0
      • L LongRange Shooter

        Here is one way of stopping your thread, making sure your current execution ends and you do not get an AbortException. You would have something like this to establish the thread in your main processor:

        ThreadClass function = new ThreadClass(); // your process
        myThread = new ThreadStart(function.Process);

        your Process would be something like this:

        public bool ThreadWanted = true;
        ... init/constructor logic ...
        do
        {
        if ( ..no data to process.. )
        Thread.Sleep(50);
        ...function
        }
        while (ThreadWanted)
        return;

        when you want the thread to abort just do this in the thread creator logic:

        public void TerminateProcess()
        {
        function.ThreadWanted = false;
        }

        There are 10 kinds of people in the world.
        Those that read binary...
        ...and those who don't.

        B Offline
        B Offline
        Blue_Skye
        wrote on last edited by
        #3

        Hi! Thanks a lot! :)

        1 Reply Last reply
        0
        • L LongRange Shooter

          Here is one way of stopping your thread, making sure your current execution ends and you do not get an AbortException. You would have something like this to establish the thread in your main processor:

          ThreadClass function = new ThreadClass(); // your process
          myThread = new ThreadStart(function.Process);

          your Process would be something like this:

          public bool ThreadWanted = true;
          ... init/constructor logic ...
          do
          {
          if ( ..no data to process.. )
          Thread.Sleep(50);
          ...function
          }
          while (ThreadWanted)
          return;

          when you want the thread to abort just do this in the thread creator logic:

          public void TerminateProcess()
          {
          function.ThreadWanted = false;
          }

          There are 10 kinds of people in the world.
          Those that read binary...
          ...and those who don't.

          S Offline
          S Offline
          S Senthil Kumar
          wrote on last edited by
          #4

          You need to declare the bool variable with the volatile qualifier, without it, there is no guarantee that setting it to true would immediately reflect in the other thread. Regards Senthil _____________________________ My Blog | My Articles | WinMacro

          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