OLD | NEW |
| (Empty) |
1 using System; | |
2 using System.Collections.Generic; | |
3 using System.ComponentModel; | |
4 using System.Data; | |
5 using System.Diagnostics; | |
6 using System.Drawing; | |
7 using System.Linq; | |
8 using System.Text; | |
9 using System.Windows.Forms; | |
10 using Microsoft.Win32; | |
11 | |
12 namespace MsiHelp | |
13 { | |
14 public enum MainFormResultCode { | |
15 Unknown = -1, | |
16 Ok = 0, | |
17 UserCancelled, | |
18 BadArgs, | |
19 Error, | |
20 } | |
21 | |
22 public partial class MainForm : Form | |
23 { | |
24 private static readonly string kVsName = "devenv"; | |
25 | |
26 public MainForm() | |
27 { | |
28 InitializeComponent(); | |
29 ResultCode = MainFormResultCode.Unknown; | |
30 this.Visible = false; | |
31 } | |
32 | |
33 public MainFormResultCode ResultCode { get; set; } | |
34 public Exception Error { get; set; } | |
35 | |
36 protected override void OnLoad(EventArgs e) | |
37 { | |
38 this.Visible = false; | |
39 string[] args = Environment.GetCommandLineArgs(); | |
40 if (args.Length != 2) { | |
41 MessageBox.Show(this, "Oops: wrong number of arguments", "MsiHelp", Mess
ageBoxButtons.OK, MessageBoxIcon.Error); | |
42 ResultCode = MainFormResultCode.BadArgs; | |
43 Close(); | |
44 } | |
45 | |
46 if (args[1] == "test") { | |
47 this.Text = "Test Installer"; | |
48 message_.Text = "Wasting time..."; | |
49 worker_.DoWork += new DoWorkEventHandler(TestWorker); | |
50 } else if (args[1] == "killvs") { | |
51 this.Visible = true; | |
52 Text = "Close Visual Studio"; | |
53 message_.Text = "Waiting for Visual Studio to close..."; | |
54 worker_.DoWork += new DoWorkEventHandler(KillVsWorker); | |
55 } else if (args[1] == "setup") { | |
56 this.Visible = true; | |
57 Text = "Registering Packages"; | |
58 message_.Text = "Registering packages with Visual Studio..."; | |
59 worker_.DoWork += new DoWorkEventHandler(DevEnvSetup); | |
60 } | |
61 | |
62 worker_.RunWorkerAsync(); | |
63 base.OnLoad(e); | |
64 } | |
65 | |
66 void DevEnvSetup(object sender, DoWorkEventArgs e) | |
67 { | |
68 // string vsdir = Registry.GetValue("") | |
69 } | |
70 | |
71 void KillVsWorker(object sender, DoWorkEventArgs e) | |
72 { | |
73 var vsProcesses = new List<Process>(Process.GetProcessesByName(kVsName)); | |
74 | |
75 if (vsProcesses.Count == 0) { | |
76 Finish(MainFormResultCode.Ok); | |
77 } | |
78 | |
79 DialogResult autoClose = (DialogResult) Invoke(new InvokeDialogHandler( ()
=> AutoCloseMessage() )); | |
80 | |
81 try { | |
82 switch (autoClose) { | |
83 case DialogResult.Yes: | |
84 Invoke( | |
85 new InvokeHandler( | |
86 () => message_.Text = "Closing Visual Studio...")); | |
87 foreach (var vsProcess in vsProcesses) { | |
88 vsProcess.CloseMainWindow(); | |
89 } | |
90 break; | |
91 case DialogResult.No: | |
92 break; | |
93 case DialogResult.Cancel: | |
94 Finish(MainFormResultCode.UserCancelled); | |
95 break; | |
96 } | |
97 | |
98 List<Process> deaders = new List<Process>(); | |
99 while (vsProcesses.Count > 0) { | |
100 foreach (var vsProcess in vsProcesses) { | |
101 if (vsProcess.HasExited) { | |
102 deaders.Add(vsProcess); | |
103 } | |
104 } | |
105 foreach (var process in deaders) { | |
106 vsProcesses.Remove(process); | |
107 process.Dispose(); | |
108 } | |
109 deaders.Clear(); | |
110 } | |
111 } catch (Exception ex) { | |
112 Error = ex; | |
113 Finish(MainFormResultCode.Error); | |
114 return; | |
115 } | |
116 Finish(MainFormResultCode.Ok); | |
117 } | |
118 | |
119 private DialogResult AutoCloseMessage() { | |
120 return MessageBox.Show( | |
121 this, | |
122 "Installation cannot proceed until Visual Studio is closed.\n" + | |
123 "Close Visual Studio automatically?\n" + | |
124 "(Select 'Yes' to close Visual Studio automatically. Select 'No' to cl
ose it yourself. Select 'Cancel' to abort the install process.", | |
125 "Installer", | |
126 MessageBoxButtons.YesNoCancel, | |
127 MessageBoxIcon.Question); | |
128 } | |
129 | |
130 private delegate void InvokeHandler(); | |
131 | |
132 private delegate DialogResult InvokeDialogHandler(); | |
133 private void OnFinished(MainFormResultCode code) { | |
134 ResultCode = code; | |
135 Close(); | |
136 } | |
137 | |
138 private void Finish(MainFormResultCode code) { | |
139 this.Invoke(new InvokeHandler(() => OnFinished(code))); | |
140 } | |
141 | |
142 void TestWorker(object sender, DoWorkEventArgs e) | |
143 { | |
144 System.Threading.Thread.Sleep(5000); | |
145 Finish(MainFormResultCode.Ok); | |
146 } | |
147 | |
148 private void button1_Click(object sender, EventArgs e) | |
149 { | |
150 OnFinished(MainFormResultCode.UserCancelled); | |
151 } | |
152 } | |
153 } | |
OLD | NEW |