[Ironpython-users] Running IronPython WPF app on seperate thread
Jackie Sproat
JackieSproat at smarttech.com
Fri Dec 14 19:23:19 CET 2012
Question:
How do I run the IronPython wpf app on its own thread so I can check class variables (self.age, self._textBox1) while the application is running?
Is it easier to use IronPython decorator (my Python @run_async doesn't work) or .NET Threading or IronPython threading (are STATHREADs supported in IronPython)?
And how do I define a class method that can be run without an instance of the class?
For example,
If I add new method Run_App_in_own_thread() this app will create an instance of WpfApplication1? Do I add this method to its own class and import that class in WpfApplication1 class or is there a better way?
BACKGROUND:
I created a simple IronPython wpf application in VS2010, it looks something like this:
class WpfApplication1(Window):
def __init__(self):
self.age = 36
self = wpf.LoadComponent(self, 'WpfApplication1.xaml')
self.Loaded += self.Window_Loaded
self._textBox1.Text = 'My Text'
def get_textBox1(self):
return self._textBox1
def set_textBox1(self, value):
self._textBox1 = value
textBox1 = property(get_textBox1, set_textBox1)
when a specific event is triggered on wpf UI I change self.age and self._textBox1:
def myEvent(self, group):
text = "testing"
if not System.String.IsNullOrEmpty(text):
self.set_textBox1(text) #set _textBox1
self.age = 100
Finally, I have two more WpfApplication1 class methods where the goal to at pass on the property values (for test/verification):
def Provide_Age(self):
return self.age
def Provide_Text(self):
self.get_textBox1() #return _textBox1
Question:
How do I run the wpf app?
Note: I want it on its own thread or so I can check Age and text before and after UI interactions...
# I want something like this:
- if __name__ == '__main__':
-
try:
- app = WpfApplication1()#one instance of the class
- app.Run_App_on_new_Thread()
- Sleep(10) #while user interacts with UI
- #verify OUTPUT
- Age = app.Provide_Age()
- except Exception, e:
- print "Caught exception: %s" % str(e)
- raise
-
Background:
- Run from VS2010 - wpf app runs and takes input - I can step through and see age and textBox1 being set in self > .dict > age ...
- Run from __main__ using simply: Application().Run(WpfApplication1()) with command:
Ø ipy WpfApplication1.py
wpf app runs and take input but, it is not on different Thread so I can no longer use command line or continue in __main__ to
call Provide_Age(), Provide_Text()...
-
- tried to run using the following BUT IT DIDN'T WORK - APP DOESN'T even start and no exception thrown:
- def Run_Application_Thread(self):
-
- ''' WARNING: Not sure if I can run multiple instances of wpf app...'''
- try:
- thrd = Thread(ThreadStart(self.thread_start))
- thrd.ApartmentState = ApartmentState.STA
- thrd.IsBackground = True
- thrd.Start()
- except Exception, e:
- print "Caught exception: %s" % str(e)
- raise
- clr.SetCommandDispatcher(self.DispatchConsoleCommand)
-
-
- def thread_start(self):
- try:
- self.Application().Run(WpfApplication1())
- finally:
- print "finally"
- clr.SetCommandDispatcher(None)
-
- if __name__ == '__main__':
-
try:
- app = WpfApplication1()#one instance of the class
- app.Run_Application_Thread() #creates another instance of the class...which I think is where my problem is ...
- #but I NEED AN INSTACE TO CALL THE CLASS METHOD!!!!!!!!!!!!!!!!!!??????????
- except Exception, e:
- print "Caught exception: %s" % str(e)
- raise
-
-
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/ironpython-users/attachments/20121214/7a2c9184/attachment.html>
More information about the Ironpython-users
mailing list