DragonHeart000 / TwitchChatHotKeys

TwitchChatHotKeys is a program that allows Twitch Streamers to bind any key on their keybaord to say anything in twitch chat. Most common uses would be for running a commercial, moderating chat with timeouts/bans, or putting chat in followers/subscribers only mode, but anything can be done with it.
https://www.thk.chat
GNU General Public License v3.0
5 stars 1 forks source link

Program fails to terminate when closed if running #4

Closed DragonHeart000 closed 6 years ago

DragonHeart000 commented 6 years ago

Description When the program is closed it does not terminate all threads and such the program keeps running in the background and will respond to macros that are bound.

Version 0.2.0 Alpha

OS Windows 10

Screenshot/video https://i.gyazo.com/d9c11f215c14831a9c419500ba29ad76.mp4

Other Notes This will be patched out when adding minimizing to tray features where pressing the x will send it to the windows tray and closing if from there will then terminate ALL threads and make sure this issue does not stay.

Currently, the issue stands as when closing the program it just executes platform.exit(); and does not go through killing the threads that are running the bot properly.

DragonHeart000 commented 6 years ago

This has legit been driving me insane. I have a working way of doing this in twitch bot I have worked on and that works flawlessly. But now that I try copy pasting that over here it won't work. It should look like this:

twitchThread=new Thread(){
        @Override
        public void run(){
            PircBotX twitchBot=null; //Initialize twitchBot so we can access it outside of the while loop

            try { //Try catch so we can handle the interrupted exception we will throw
                while (!Thread.interrupted()){ //while loop so we can throw interrupted exception in order to close twitchBot
                //Cut out code
                }
            } catch (Exception e){
                //We may ignore it since we only wanted it so we can break the loop
            } finally { //Finally statement to make sure we dispose of the resources properly
                twitchBot.close(); //Close the twitchBot so the thread my close properly.
            }
        }
    };

For the thread and for killing it have a method call on exit that runs this code:

@SuppressWarnings("deprecation")
    public void killThreads(){
        try {
            if (twitchThread.isAlive()){
                System.out.println("twitch thread stoped");
                twitchThread.interrupt(); //stops the threads with the bot running to prevent resource leaks.\
                if (twitchThread.isAlive()){ //Checks if the thread is still alive
                    twitchThread.stop(); //Force closes thread the unsafe way if the thread was not closing as it should have before.
                }
            }
        } catch (Exception e){
            //Try catch in order to prevent errors in event that the program is terminated via this method before start() is called
        }
    }

In my OsuBot program this solution works just fine. I have tried for so long to get this to work in here and it never will interrupt. After I call thread.interrupt() the thread is still alive so it calls thread.stop() and it still stays alive. I will come back to this issue after some rest with a fresh start.

DragonHeart000 commented 6 years ago

I have a version that disconnects the bot from the server and kills the thread but there is still a resource leak somewhere so the program remains running but just throws errors and never messages twitch chat. Working on this now

DragonHeart000 commented 6 years ago

The leak was in the KeyListener class, I have taken care of it and am just cleaning up the code and then pushing the commit.