Force termination of a thread
-
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 dot.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 -
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 dot.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 //OtisWhat 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 thingt.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 -
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 thingt.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? //OtisWell, 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