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. Force termination of a thread

Force termination of a thread

Scheduled Pinned Locked Moved C#
questionsysadminhelp
3 Posts 1 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.
  • O Offline
    O Offline
    Otis_69
    wrote on last edited by
    #1

    OMG, this is my second time writing this, cause last time i wasnt logged in, and i clicked submit, and it said i needed to login, so i hit back to copy/paste my post into notepad, and it was gone... arrgh. Anyways: I have a class, lets call it "IrcBot". Now, i instanciate it like this: IrcBot mybot = new IrcBot("server","channel","nick"); now, in my main class, i make a thread (or 10) all going to the same, or different irc servers... like this: Thread t = new Thread(new ThreadStart(mybot.connect)); t.Start(); seeing as there is no way to terminate the connection from the bot to the irc server, i cant terminate the thread, cause the thread is still doing something... In english, when i do t.Abort(); it doesnt stop and the bot is still in the irc server in that channel... How can i force the thread to stop regardless of whether its doing something or not? Thanks for the help :P //Otis

    O 1 Reply Last reply
    0
    • O Otis_69

      OMG, this is my second time writing this, cause last time i wasnt logged in, and i clicked submit, and it said i needed to login, so i hit back to copy/paste my post into notepad, and it was gone... arrgh. Anyways: I have a class, lets call it "IrcBot". Now, i instanciate it like this: IrcBot mybot = new IrcBot("server","channel","nick"); now, in my main class, i make a thread (or 10) all going to the same, or different irc servers... like this: Thread t = new Thread(new ThreadStart(mybot.connect)); t.Start(); seeing as there is no way to terminate the connection from the bot to the irc server, i cant terminate the thread, cause the thread is still doing something... In english, when i do t.Abort(); it doesnt stop and the bot is still in the irc server in that channel... How can i force the thread to stop regardless of whether its doing something or not? Thanks for the help :P //Otis

      O Offline
      O Offline
      Otis_69
      wrote on last edited by
      #2

      What i did now, is i made an instance of a timer within my thread, and set the interval to 1 millisecond... System.timers.timer t = new System.timers.timer(1); Then i made the event handler thing t.Elapsed += new System.Timers.ElapsedEventHandler(t_Elapsed); and the elapsed method looks like this: private void t_Elapsed(object sender, System.Timers.ElapsedEventArgs e) { if(System.Threading.Thread.CurrentThread.ThreadState.Equals(System.Threading.ThreadState.AbortRequested)) { connection.Disconnect("Gotta go!"); } } Why wont that terminate the connection & die when i call abort from the main thread? //Otis

      O 1 Reply Last reply
      0
      • O Otis_69

        What i did now, is i made an instance of a timer within my thread, and set the interval to 1 millisecond... System.timers.timer t = new System.timers.timer(1); Then i made the event handler thing t.Elapsed += new System.Timers.ElapsedEventHandler(t_Elapsed); and the elapsed method looks like this: private void t_Elapsed(object sender, System.Timers.ElapsedEventArgs e) { if(System.Threading.Thread.CurrentThread.ThreadState.Equals(System.Threading.ThreadState.AbortRequested)) { connection.Disconnect("Gotta go!"); } } Why wont that terminate the connection & die when i call abort from the main thread? //Otis

        O Offline
        O Offline
        Otis_69
        wrote on last edited by
        #3

        Well, i finally figured it out... In the IrcBot class, i made the variable holding the connection public. Then, before i started the thread, i grabbed that connection, and stored it in a variable. Then when i want to abort the thread, i use the variable and disconnect, and then abort :D It is kinda crude, but it works :D //Otis

        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