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. Threading, just professionall please...

Threading, just professionall please...

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

    hi friend's i have an application that work with threading. in load_form event of the form i create an instance a thread object and run it, now my problem is , i want to kill the thread on another event such as button_click event on this form and i cant do it. i can not create my thread public, because the number of thread should be create is not certain in my allpication, how can i do it, please sya me a solution to solve it... thanks alot

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

    M L 2 Replies Last reply
    0
    • M mr mohsen

      hi friend's i have an application that work with threading. in load_form event of the form i create an instance a thread object and run it, now my problem is , i want to kill the thread on another event such as button_click event on this form and i cant do it. i can not create my thread public, because the number of thread should be create is not certain in my allpication, how can i do it, please sya me a solution to solve it... thanks alot

      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 only reliable way to kill a thread is to let the thread terminate normally. This means you need a way to notify a thread that it needs to terminate, and provide a way to wait for the thread to terminate if necessary. There's synchronization objects you can use. For example, EventWaitHandle. There's no need to "create my thread public", but you may need to keep contexts to your created threads somewhere, like in a collection in your form class. Mark

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

      1 Reply Last reply
      0
      • M mr mohsen

        hi friend's i have an application that work with threading. in load_form event of the form i create an instance a thread object and run it, now my problem is , i want to kill the thread on another event such as button_click event on this form and i cant do it. i can not create my thread public, because the number of thread should be create is not certain in my allpication, how can i do it, please sya me a solution to solve it... thanks alot

        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

        One way to gracefully terminate a thread is to set a volatile bool variable to some value and check this value inside the other thread, like this:

        public void ButtonClickHandler(Object sender, EventArgs args)
        {
        requestStopThread = true; // this is a volatile bool variable
        }

        Inside your thread you need to check if this variable will be set to true and then terminate the thread in a correct way:

        while(!requestStopThread)
        {
        // do some stuff, terminate otherwise
        }

        or

        // do something
        ...

        if(requestStopThread)
        // terminate

        // do something
        ...

        regards

        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