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 Aorting

Thread Aorting

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.
  • H Offline
    H Offline
    HexaDeveloper
    wrote on last edited by
    #1

    hi all, i tried to abort thread but when i make myThread.Abort(); it give me an exception of aborting thread so what can i do to avoid this exception another thing is that Suspend Method is obsolete(help said) what is the alternative third and last: when i declare thread as member data in class it refuses to identify the name of the method i used to initialize like: Thread packetsMovingThread = new Thread(new ThreadStart(threadProcedure)); private threadProcedure() { my method } thanx Generator

    L 1 Reply Last reply
    0
    • H HexaDeveloper

      hi all, i tried to abort thread but when i make myThread.Abort(); it give me an exception of aborting thread so what can i do to avoid this exception another thing is that Suspend Method is obsolete(help said) what is the alternative third and last: when i declare thread as member data in class it refuses to identify the name of the method i used to initialize like: Thread packetsMovingThread = new Thread(new ThreadStart(threadProcedure)); private threadProcedure() { my method } thanx Generator

      L Offline
      L Offline
      Leslie Sanford
      wrote on last edited by
      #2

      HexaDeveloper wrote:

      i tried to abort thread but when i make myThread.Abort(); it give me an exception of aborting thread so what can i do to avoid this exception

      Allow the thread to end naturally. Typically, a thread method has a loop that it runs until it completes its task.

      private void ThreadMethod
      {
      while(notDone)
      {
      // Do stuff here...
      }
      }

      HexaDeveloper wrote:

      another thing is that Suspend Method is obsolete(help said) what is the alternative

      Hmm, well you could use locking to pause a thread:

      private void ThreadMethod
      {
      lock(lockObject)
      {
      Monitor.Wait(lockObject);
      }

      while(!done)
      {
          // Do stuff here...
      }       
      

      }

      And at the point in which you create the thread:

      Thread myThread = new Thread(ThreadMethod);

      myThread.Start();

      lock(lockObject)
      {
      Monitor.Pulse(lockObject);
      }

      This will create a thread and start it. The thread runs until it reaches the Monitor.Wait(lockObject) statement. At that point, it's up to you to wake up the thread and let it do its thing. This is done above with the Monitor.Pulse(lockObject) statement. Now, often times you want a thread to wait periodically instead of just when it's started up. So we can put a Wait inside the loop:

      private void ThreadMethod
      {
      lock(lockObject)
      {
      while(!done)
      {
      Monitor.Wait(lockObject);

              if(done)
              {
                  break;
              }
      
              // Do stuff here...
          }       
      }
      

      }

      Then it's up to your application to periodically wake up the thread to do its work. Hope this helps.

      HexaDeveloper wrote:

      third and last: when i declare thread as member data in class it refuses to identify the name of the method i used to initialize like:

      This question I don't understand. Can you elaborate?

      H 1 Reply Last reply
      0
      • L Leslie Sanford

        HexaDeveloper wrote:

        i tried to abort thread but when i make myThread.Abort(); it give me an exception of aborting thread so what can i do to avoid this exception

        Allow the thread to end naturally. Typically, a thread method has a loop that it runs until it completes its task.

        private void ThreadMethod
        {
        while(notDone)
        {
        // Do stuff here...
        }
        }

        HexaDeveloper wrote:

        another thing is that Suspend Method is obsolete(help said) what is the alternative

        Hmm, well you could use locking to pause a thread:

        private void ThreadMethod
        {
        lock(lockObject)
        {
        Monitor.Wait(lockObject);
        }

        while(!done)
        {
            // Do stuff here...
        }       
        

        }

        And at the point in which you create the thread:

        Thread myThread = new Thread(ThreadMethod);

        myThread.Start();

        lock(lockObject)
        {
        Monitor.Pulse(lockObject);
        }

        This will create a thread and start it. The thread runs until it reaches the Monitor.Wait(lockObject) statement. At that point, it's up to you to wake up the thread and let it do its thing. This is done above with the Monitor.Pulse(lockObject) statement. Now, often times you want a thread to wait periodically instead of just when it's started up. So we can put a Wait inside the loop:

        private void ThreadMethod
        {
        lock(lockObject)
        {
        while(!done)
        {
        Monitor.Wait(lockObject);

                if(done)
                {
                    break;
                }
        
                // Do stuff here...
            }       
        }
        

        }

        Then it's up to your application to periodically wake up the thread to do its work. Hope this helps.

        HexaDeveloper wrote:

        third and last: when i declare thread as member data in class it refuses to identify the name of the method i used to initialize like:

        This question I don't understand. Can you elaborate?

        H Offline
        H Offline
        HexaDeveloper
        wrote on last edited by
        #3

        hi, i mean when i write Thread myThread = new Thread(ThreadMethod); it said [Error 1 A field initializer cannot reference the nonstatic field, method, or property 'Interface.mainScreen.threadProcedure()' C:\Documents and Settings\Administrator\Desktop\Well Formed Interface_thread\Interface\mainScreen.cs 38 49 Interface ] and i am actually want to know the reason that when i used abort it makes exception and i want to ask if i can make the lockobject my thread name thanx -- modified at 12:02 Wednesday 11th April, 2007 Generator -- modified at 14:19 Wednesday 11th April, 2007

        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