 
 @objc public class KHScriptThread: CNPipeThread, KHThreadProtocol
 {
+	public enum ThreadState {
+		case Initial
+		case Running
+		case Finished
+	}
+
 	public static let EnvironmentItem	= "_env"
 
 	private var mContext:			KEContext
 	private var mScripts:			Array<URL>
 	private var mArguments:			Array<String>
 	private var mTerminationStatus:		Int32
+	private var mThreadState:		ThreadState
 
 	public init(virtualMachine vm: JSVirtualMachine, shellInterface intf: CNShellInterface, environment env: CNShellEnvironment, console cons: CNConsole, config conf: KHConfig){
 		mContext		= KEContext(virtualMachine: vm)
 		mScripts		= []
 		mArguments		= []
 		mTerminationStatus	= 1
+		mThreadState		= .Initial
 		super.init(interface: intf, environment: env, console: cons, config: conf)
 
 		/* Compile the context */

		guard compiler.compile(context: mContext, sourceFiles: mScripts, console: console, config: conf) else {
			console.error(string: "Failed to compile  user scripts")
			mTerminationStatus = 1
			return
		}


		} else {
			mTerminationStatus = 0
		}
		mThreadState = .Finished
	}

	public func waitUntilExit() {
		while self.threadState != .Finished {
			Thread.sleep(forTimeInterval: 1/10000.0) // 0.1ms
		}
	}
}

