I've tried that, but when I shutdown the application the ssh thread remains and I have to kill it manually. Is there a way to make sure the thread is cleaned up?
Yes. You can keep track of the pids of the ssh runs. I, however, am not the right monk to throw down an example of how to do that.
Try to distill it down to a snippet that runs something you're doing and post it as a new SoPW asking how to keep track of and kill/close the ssh when you're done with it.