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. how kill a thread that is on wait state??????

how kill a thread that is on wait state??????

Scheduled Pinned Locked Moved C#
helpquestion
6 Posts 4 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.
  • M Offline
    M Offline
    mr mohsen
    wrote on last edited by
    #1

    hi i am working on a project that neede to use multi threading in a method i use Monitor.Enter(Object) for define a critical area. now if few thread wants to execute the method , monitor.Enter avoid to run Synchronize the method by all thread at the same time, so few threads will be on wait state. now my problem is may be user wants to cancell the execution of a thread that is on wait state how can i kill the thread.??? if i call Thread.CurrentThread..Interrupt() below error appears: "cannot kill a thread that was in wait state" :-\ this is not complete error, this is just shortest of error. thank you

    nobody help you... you have to help you yourself and this is success way.

    M L D 3 Replies Last reply
    0
    • M mr mohsen

      hi i am working on a project that neede to use multi threading in a method i use Monitor.Enter(Object) for define a critical area. now if few thread wants to execute the method , monitor.Enter avoid to run Synchronize the method by all thread at the same time, so few threads will be on wait state. now my problem is may be user wants to cancell the execution of a thread that is on wait state how can i kill the thread.??? if i call Thread.CurrentThread..Interrupt() below error appears: "cannot kill a thread that was in wait state" :-\ this is not complete error, this is just shortest of error. thank you

      nobody help you... you have to help you yourself and this is success way.

      M Offline
      M Offline
      Mark Salsbery
      wrote on last edited by
      #2

      The thread that currently owns the lock should use some mechanism to indicate a "cancelled" state and then release the lock. After acquiring a lock, waiting threads should first look at the cancelled state before proceeding. Mark

      Mark Salsbery Microsoft MVP - Visual C++ :java:

      M 1 Reply Last reply
      0
      • M mr mohsen

        hi i am working on a project that neede to use multi threading in a method i use Monitor.Enter(Object) for define a critical area. now if few thread wants to execute the method , monitor.Enter avoid to run Synchronize the method by all thread at the same time, so few threads will be on wait state. now my problem is may be user wants to cancell the execution of a thread that is on wait state how can i kill the thread.??? if i call Thread.CurrentThread..Interrupt() below error appears: "cannot kill a thread that was in wait state" :-\ this is not complete error, this is just shortest of error. thank you

        nobody help you... you have to help you yourself and this is success way.

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #3

        You might try to use Monitor.TryEnter. This does not block, however you have to do this in a loop until you finally get the lock. This is called polling/busy waiting and can cost some CPU cycles, but maybe it works better in your case.

        while(!Monitor.TryEnter(obj) || !shouldStop)
        {
        Thread.Sleep(1000);
        }

        regards

        1 Reply Last reply
        0
        • M mr mohsen

          hi i am working on a project that neede to use multi threading in a method i use Monitor.Enter(Object) for define a critical area. now if few thread wants to execute the method , monitor.Enter avoid to run Synchronize the method by all thread at the same time, so few threads will be on wait state. now my problem is may be user wants to cancell the execution of a thread that is on wait state how can i kill the thread.??? if i call Thread.CurrentThread..Interrupt() below error appears: "cannot kill a thread that was in wait state" :-\ this is not complete error, this is just shortest of error. thank you

          nobody help you... you have to help you yourself and this is success way.

          D Offline
          D Offline
          dan sh
          wrote on last edited by
          #4

          I am not sure about the article but even MSDN says that you cannot take full control of a thread. the mostr you can do is to make it wait. I am not sure about article but I remember these things. Hope it helps.

          "If you had to identify, in one word, the reason why the human race has not achieved, and never will achieve, its full potential, that word would be 'meetings'." - Dave Barry

          1 Reply Last reply
          0
          • M Mark Salsbery

            The thread that currently owns the lock should use some mechanism to indicate a "cancelled" state and then release the lock. After acquiring a lock, waiting threads should first look at the cancelled state before proceeding. Mark

            Mark Salsbery Microsoft MVP - Visual C++ :java:

            M Offline
            M Offline
            mr mohsen
            wrote on last edited by
            #5

            thanks alot mark but whats that mechanism? i tray alot but cannot find a solution. please tell me completely

            nobody help you... you have to help you yourself and this is success way.

            M 1 Reply Last reply
            0
            • M mr mohsen

              thanks alot mark but whats that mechanism? i tray alot but cannot find a solution. please tell me completely

              nobody help you... you have to help you yourself and this is success way.

              M Offline
              M Offline
              Mark Salsbery
              wrote on last edited by
              #6

              Add a boolean property to the class perhaps? Mark

              Mark Salsbery Microsoft MVP - Visual C++ :java:

              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