PageRenderTime 10ms CodeModel.GetById 2ms app.highlight 6ms RepoModel.GetById 1ms app.codeStats 0ms

/IronPython_Main/Languages/IronPython/Public/Tutorial/winforms.py

#
Python | 76 lines | 33 code | 8 blank | 35 comment | 1 complexity | 725e16fb9b0345de084465398bda1ee4 MD5 | raw file
 1#####################################################################################
 2#
 3#  Copyright (c) Microsoft Corporation. All rights reserved.
 4#
 5# This source code is subject to terms and conditions of the Apache License, Version 2.0. A 
 6# copy of the license can be found in the License.html file at the root of this distribution. If 
 7# you cannot locate the  Apache License, Version 2.0, please send an email to 
 8# ironpy@microsoft.com. By using this source code in any fashion, you are agreeing to be bound 
 9# by the terms of the Apache License, Version 2.0.
10#
11# You must not remove this notice, or any other, from this software.
12#
13#
14#####################################################################################
15
16import clr
17clr.AddReferenceByPartialName("System.Windows.Forms")
18clr.AddReferenceByPartialName("System.Drawing")
19clr.AddReferenceByPartialName("IronPython")
20clr.AddReferenceByPartialName("Microsoft.Scripting")
21
22from System.Drawing import Size
23from System.Windows.Forms import Form, Application
24from System.Threading import Thread
25from System.Threading import ThreadStart
26from System.Threading import AutoResetEvent
27import IronPython
28
29# We support interactive development of Windows Forms by creating another
30# thread on which we execute all of the IronPython console's input.  This
31# allows the form to execute on another thread where a message pump can
32# independently handle input and allow the form to repaint.  To make this work
33# we do two things.  First we create a dummy hidden form on the other thread
34# which gives us a handle for marshalling the execution of console input onto
35# the thread where we will develop our real form.  Second, we supply an
36# alternative function for the IronPython console to run for executing console
37# input.  Our alternative function simply invokes the input on the other
38# thread.
39
40
41are = AutoResetEvent(False)
42
43def thread_proc():
44    try:
45        global dispatcher
46        global are
47        # Create the dummy control, and show then hide it to get Windows Forms
48        # to initialize it.
49        dispatcher = Form(Size = Size(0,0))
50        dispatcher.Show()
51        dispatcher.Hide()
52        # Signal that the thread running thread_proc is ready for the main
53        # thread to send input to it.
54        are.Set()
55        # Start the message loop.
56        Application.Run()
57    finally:
58        # In case thread_proc's thread dies, restore the default IronPython
59        # console execution behavior.
60        clr.SetCommandDispatcher(None)
61
62def DispatchConsoleCommand(consoleCommand):
63    if consoleCommand:
64        # consoleCommand is a delegate for a dynamic method that embodies the
65        # input expression from the user.  Run it on the other thread.
66        dispatcher.Invoke(consoleCommand)
67    else:
68        Application.Exit()
69
70t = Thread(ThreadStart(thread_proc))
71t.IsBackground = True
72t.Start()
73# Don't establish the alternative input execution behavior until the other
74# thread is ready.  Note, 'are' starts out unsignalled.
75are.WaitOne()
76clr.SetCommandDispatcher(DispatchConsoleCommand)