import sys
import time
import threading
import maya.utils as utils
import maya.mel as mel

class TimerObj(threading.Thread):
	"""Simple Autosave mechanism"""
	def __init__(self):
		self.shouldRun = False
		threading.Thread.__init__(self)
	def run(self):
		while self.shouldRun:
			time.sleep(self.runTime)
	  		utils.executeDeferred(timedEvent)

#useful for stopping a thread
myTimer = TimerObj()

def timedEvent():
	print "Autosaving file"
	mel.eval( 'FileMenu_SaveItem;' )

def sched(runTime=300):
	myTimer.runTime = runTime
	myTimer.shouldRun = True
	myTimer.start()

def start():
	myTimer.shouldRun = True

def stop():
	# make the thread stop
	myTimer.shouldRun = False