from time import sleep

import threading, Queue, time

# import SYSTEM DLL:
#from CLR.System import Xml
from CLR.System.Xml import XmlDocument
import CLR.System as System

# import DLL: ATFCommunicator.dll
from CLR.System.Reflection import Assembly
a = Assembly.LoadWithPartialName("ATFCommunicator")
from CLR.ATFCommunicator import ATFCommunicatorUtilities


class MessageRouter( threading.Thread ):

    #=============================================================================================
    # Constructor, Setting initial variables
    #=============================================================================================
    def __init__( self):

        print "######init MessageRouter"
        self._stopEvent = threading.Event()
        threading.Thread.__init__( self, name = 'Child Thread' )


    #=============================================================================================
    # Main Control Loop
    #=============================================================================================
    def run( self ):

        print "######run MessageRouter - Child Thread"

        print "%s starts" % ( self.getName() )

        print "###### Calling from Python (Child Thread) " + str(ATFCommunicatorUtilities.GetOne())

        print "%s ends" % ( self.getName() )


    #=============================================================================================
    # Stop the thread
    #=============================================================================================
    def terminate( self ):

        self._stopEvent.set()


print "Main Thread - GetOne() " + str(ATFCommunicatorUtilities.GetOne())
_MessageRouter = MessageRouter()
_MessageRouter.start()
print "Main Thread - After MessageRouter Start " + str(ATFCommunicatorUtilities.GetOne())
timeout = 30.0
_PullSleepPeriod = 0.2   
while( timeout > 0 ):
    print "Main Thread - Inside  WHILE loop ====" + str(timeout)
    time.sleep( _PullSleepPeriod )
    timeout -= _PullSleepPeriod
    print "Main Thread - Timed out waiting for PM to respond."

print "Main Thread - Program End" + str(ATFCommunicatorUtilities.GetOne())




