diff options
Diffstat (limited to 'src/samples/Dtf/EmbeddedUI/SampleEmbeddedUI.cs')
-rw-r--r-- | src/samples/Dtf/EmbeddedUI/SampleEmbeddedUI.cs | 132 |
1 files changed, 132 insertions, 0 deletions
diff --git a/src/samples/Dtf/EmbeddedUI/SampleEmbeddedUI.cs b/src/samples/Dtf/EmbeddedUI/SampleEmbeddedUI.cs new file mode 100644 index 00000000..9b26bef5 --- /dev/null +++ b/src/samples/Dtf/EmbeddedUI/SampleEmbeddedUI.cs | |||
@@ -0,0 +1,132 @@ | |||
1 | // Copyright (c) .NET Foundation and contributors. All rights reserved. Licensed under the Microsoft Reciprocal License. See LICENSE.TXT file in the project root for full license information. | ||
2 | |||
3 | namespace WixToolset.Dtf.Samples.EmbeddedUI | ||
4 | { | ||
5 | using System; | ||
6 | using System.Collections.Generic; | ||
7 | using System.Configuration; | ||
8 | using System.Threading; | ||
9 | using System.Windows; | ||
10 | using System.Windows.Threading; | ||
11 | using WixToolset.Dtf.WindowsInstaller; | ||
12 | using Application = System.Windows.Application; | ||
13 | |||
14 | public class SampleEmbeddedUI : IEmbeddedUI | ||
15 | { | ||
16 | private Thread appThread; | ||
17 | private Application app; | ||
18 | private SetupWizard setupWizard; | ||
19 | private ManualResetEvent installStartEvent; | ||
20 | private ManualResetEvent installExitEvent; | ||
21 | |||
22 | /// <summary> | ||
23 | /// Initializes the embedded UI. | ||
24 | /// </summary> | ||
25 | /// <param name="session">Handle to the installer which can be used to get and set properties. | ||
26 | /// The handle is only valid for the duration of this method call.</param> | ||
27 | /// <param name="resourcePath">Path to the directory that contains all the files from the MsiEmbeddedUI table.</param> | ||
28 | /// <param name="internalUILevel">On entry, contains the current UI level for the installation. After this | ||
29 | /// method returns, the installer resets the UI level to the returned value of this parameter.</param> | ||
30 | /// <returns>True if the embedded UI was successfully initialized; false if the installation | ||
31 | /// should continue without the embedded UI.</returns> | ||
32 | /// <exception cref="InstallCanceledException">The installation was canceled by the user.</exception> | ||
33 | /// <exception cref="InstallerException">The embedded UI failed to initialize and | ||
34 | /// causes the installation to fail.</exception> | ||
35 | public bool Initialize(Session session, string resourcePath, ref InstallUIOptions internalUILevel) | ||
36 | { | ||
37 | if (session != null) | ||
38 | { | ||
39 | if ((internalUILevel & InstallUIOptions.Full) != InstallUIOptions.Full) | ||
40 | { | ||
41 | // Don't show custom UI when the UI level is set to basic. | ||
42 | return false; | ||
43 | |||
44 | // An embedded UI could display an alternate dialog sequence for reduced or | ||
45 | // basic modes, but it's not implemented here. We'll just fall back to the | ||
46 | // built-in MSI basic UI. | ||
47 | } | ||
48 | |||
49 | if (String.Equals(session["REMOVE"], "All", StringComparison.OrdinalIgnoreCase)) | ||
50 | { | ||
51 | // Don't show custom UI when uninstalling. | ||
52 | return false; | ||
53 | |||
54 | // An embedded UI could display an uninstall wizard, it's just not imlemented here. | ||
55 | } | ||
56 | } | ||
57 | |||
58 | // Start the setup wizard on a separate thread. | ||
59 | this.installStartEvent = new ManualResetEvent(false); | ||
60 | this.installExitEvent = new ManualResetEvent(false); | ||
61 | this.appThread = new Thread(this.Run); | ||
62 | this.appThread.SetApartmentState(ApartmentState.STA); | ||
63 | this.appThread.Start(); | ||
64 | |||
65 | // Wait for the setup wizard to either kickoff the install or prematurely exit. | ||
66 | int waitResult = WaitHandle.WaitAny(new WaitHandle[] { this.installStartEvent, this.installExitEvent }); | ||
67 | if (waitResult == 1) | ||
68 | { | ||
69 | // The setup wizard set the exit event instead of the start event. Cancel the installation. | ||
70 | throw new InstallCanceledException(); | ||
71 | } | ||
72 | else | ||
73 | { | ||
74 | // Start the installation with a silenced internal UI. | ||
75 | // This "embedded external UI" will handle message types except for source resolution. | ||
76 | internalUILevel = InstallUIOptions.NoChange | InstallUIOptions.SourceResolutionOnly; | ||
77 | return true; | ||
78 | } | ||
79 | } | ||
80 | |||
81 | /// <summary> | ||
82 | /// Processes information and progress messages sent to the user interface. | ||
83 | /// </summary> | ||
84 | /// <param name="messageType">Message type.</param> | ||
85 | /// <param name="messageRecord">Record that contains message data.</param> | ||
86 | /// <param name="buttons">Message box buttons.</param> | ||
87 | /// <param name="icon">Message box icon.</param> | ||
88 | /// <param name="defaultButton">Message box default button.</param> | ||
89 | /// <returns>Result of processing the message.</returns> | ||
90 | public MessageResult ProcessMessage(InstallMessage messageType, Record messageRecord, | ||
91 | MessageButtons buttons, MessageIcon icon, MessageDefaultButton defaultButton) | ||
92 | { | ||
93 | // Synchronously send the message to the setup wizard window on its thread. | ||
94 | object result = this.setupWizard.Dispatcher.Invoke(DispatcherPriority.Send, | ||
95 | new Func<MessageResult>(delegate() | ||
96 | { | ||
97 | return this.setupWizard.ProcessMessage(messageType, messageRecord, buttons, icon, defaultButton); | ||
98 | })); | ||
99 | return (MessageResult) result; | ||
100 | } | ||
101 | |||
102 | /// <summary> | ||
103 | /// Shuts down the embedded UI at the end of the installation. | ||
104 | /// </summary> | ||
105 | /// <remarks> | ||
106 | /// If the installation was canceled during initialization, this method will not be called. | ||
107 | /// If the installation was canceled or failed at any later point, this method will be called at the end. | ||
108 | /// </remarks> | ||
109 | public void Shutdown() | ||
110 | { | ||
111 | // Wait for the user to exit the setup wizard. | ||
112 | this.setupWizard.Dispatcher.BeginInvoke(DispatcherPriority.Normal, | ||
113 | new Action(delegate() | ||
114 | { | ||
115 | this.setupWizard.EnableExit(); | ||
116 | })); | ||
117 | this.appThread.Join(); | ||
118 | } | ||
119 | |||
120 | /// <summary> | ||
121 | /// Creates the setup wizard and runs the application thread. | ||
122 | /// </summary> | ||
123 | private void Run() | ||
124 | { | ||
125 | this.app = new Application(); | ||
126 | this.setupWizard = new SetupWizard(this.installStartEvent); | ||
127 | this.setupWizard.InitializeComponent(); | ||
128 | this.app.Run(this.setupWizard); | ||
129 | this.installExitEvent.Set(); | ||
130 | } | ||
131 | } | ||
132 | } | ||