Say I want to execute two sections of code each 3 seconds and 0.1 second at the same time. How should I do that?
I will use a VBScript as an example:
Sub ScriptStart
View.SetTimerInterval1(3000)
View.SetTimerInterval2(100)
View.EnableTimer1(True)
View.EnableTimer2(True)
End Sub
Sub ScriptStop
View.EnableTimer1(False)
View.EnableTimer2(False)
Sub OnScriptTimer1
dosomething
End Sub
Sub OnScriptTimer2
dosomethingelse
End Sub
The code should let two on script timers execute do_something and do_something_else every 3 seconds and 0.1 second. How can I do it in Python? I used time.sleep()
and while True
loop to set the timer interval, but how do I implement two timers at the same time? I tried to use thread in Python:
def OnProjectRun:
t1 = threading.Thread(target=OnScriptTimer1)
t2 = threading.Thread(target=OnScriptTimer2)
t1.start()
t2.start()
def OnScriptTimer1:
dosomething()
def OnScriptTimer2:
dosomethingelse()
def dosomething:
#acutual code
def dosomethingelse:
#actual code
Then I get the error:
Traceback (most recent call last):
File "C:\Python25\Lib\threading.py", line 486, in __bootstrap_inner
self.run()
File "C:\Python25\Lib\threading.py", line 446, in run
self.__target(*self.__args, **self.__kwargs)
File "PyEditView1", line 314, in OnScriptTimer2
File "PyEditView1", line 230, in Get_FieldValue
File "C:\Python25\lib\site-packages\win32com\client\__init__.py", line 463, in __getattr__
return self._ApplyTypes_(*args)
File "C:\Python25\lib\site-packages\win32com\client\__init__.py", line 456, in _ApplyTypes_
self._oleobj_.InvokeTypes(dispid, 0, wFlags, retType, argTypes, *args),
com_error: (-2147417842, 'The application called an interface that was marshalled for a different thread.', None, None)
In this case, Get_FieldValue is dosomethingelse.