From 6af814b655f9769bd7cdc8e6877cea7b176627db Mon Sep 17 00:00:00 2001 From: the_fiddler Date: Sun, 2 Sep 2007 00:02:10 +0000 Subject: [PATCH] Example names are now more readable (ExampleLauncher.Load()) ExampleLauncher behavior is now more consistent when launching GameWindow and WinForm examples. The launcher becomes invisible, and the launched examples exist in the same thread as the launcher. Better documentation for IExample.cs --- Source/Examples/ExampleLauncher.cs | 160 ++++++++++++++++------------- Source/Examples/IExample.cs | 4 +- 2 files changed, 92 insertions(+), 72 deletions(-) diff --git a/Source/Examples/ExampleLauncher.cs b/Source/Examples/ExampleLauncher.cs index 8b9f291b..28ccaff5 100644 --- a/Source/Examples/ExampleLauncher.cs +++ b/Source/Examples/ExampleLauncher.cs @@ -29,49 +29,41 @@ namespace Examples { public partial class ExampleLauncher : Form { - /// - /// The main entry point for the application. - /// - [STAThread] - static void Main() - { - try - { - if (File.Exists("debug.log")) - File.Delete("debug.log"); - } - catch (Exception e) - { - MessageBox.Show("Could not access debug.log", e.ToString()); - } - - Debug.Listeners.Clear(); - Debug.Listeners.Add(new TextWriterTraceListener("debug.log")); - Debug.Listeners.Add(new ConsoleTraceListener()); - Debug.AutoFlush = true; - - try - { - using (Form exampleLauncher = new ExampleLauncher()) - { - Application.EnableVisualStyles(); - Application.Run(exampleLauncher); - } - } - finally - { - Debug.Flush(); - Debug.Close(); - Trace.Flush(); - Trace.Close(); - } - } + SortedList examples = new SortedList(); public ExampleLauncher() { InitializeComponent(); } + public void ExampleLauncher_Load(object sender, EventArgs e) + { + // Get all examples + Type[] types = Assembly.GetExecutingAssembly().GetTypes(); + foreach (Type type in types) + { + if (type.GetInterface("IExample") != null) + { + examples.Add( + type.Namespace.Replace("Examples.", String.Empty) + " " + + type.Name.Substring(1, 2) + ": " + + type.Name.Substring(3).Replace('_', ' '), + type.Namespace + "." + type.Name); + } + } + + foreach (string s in examples.Keys) + listBox1.Items.Add(s); + + // Select first item + if (listBox1.Items.Count > 0) + { + this.listBox1.SelectedIndex = 0; + } + } + + delegate void LaunchDelegate(object example); + void Launch(object example) { Type ex = example as Type; @@ -81,32 +73,8 @@ namespace Examples } catch (Exception expt) { - MessageBox.Show(String.Format("Stacktrace:{0}{1}{0}{0}Inner exception:{0}{2}", - System.Environment.NewLine, expt.StackTrace, expt.InnerException), expt.Message); - } - } - - public void ExampleLauncher_Load(object sender, EventArgs e) - { - SortedList sl = new SortedList(); - - // Get all examples - Type[] types = Assembly.GetExecutingAssembly().GetTypes(); - foreach (Type type in types) - { - if (type.GetInterface("IExample") != null) - { - sl.Add((type.Namespace.Replace("Examples.", String.Empty) + ": " + type.Name).Replace('_', ' '), null); - } - } - - foreach (string s in sl.Keys) - listBox1.Items.Add(s); - - // Select first item - if (listBox1.Items.Count > 0) - { - this.listBox1.SelectedIndex = 0; + MessageBox.Show(String.Format("Stacktrace:{0}{1}{0}{0}Inner exception:{0}{2}", + System.Environment.NewLine, expt.StackTrace, expt.InnerException), expt.Message); } } @@ -114,19 +82,22 @@ namespace Examples { if (listBox1.SelectedItem != null) { - Type example = - Assembly.GetExecutingAssembly().GetType( - "Examples." + listBox1.SelectedItem.ToString().Replace(": ", ".").Replace(' ', '_'), - true, - true); + Type example = Assembly.GetExecutingAssembly().GetType( + examples[listBox1.SelectedItem.ToString()], true, true); Debug.Print("Launching example: {0}", example.ToString()); + this.Visible = false; if (example.BaseType == typeof(GameWindow)) { // Start the GameWindow in a new thread - it runs its own message loop, and it would // interfere with the message loop of the ExampleLauncher. - new Thread(Launch).Start(example); + //Thread t = new Thread(Launch); + //t.Start(example); + //t = null; + Launch(example); + //IAsyncResult res = BeginInvoke(new LaunchDelegate(Launch), example); + //EndInvoke(res); } else if (example.BaseType == typeof(Form)) { @@ -134,7 +105,10 @@ namespace Examples { // In this we do not want a different thread: these examples rely on the Application.Idle // event, which would then be raised by both the ExampleLauncher thread *and* the new one! - this.AddOwnedForm((Form)example.GetConstructor(Type.EmptyTypes).Invoke(null)); + Form f = (Form)example.GetConstructor(Type.EmptyTypes).Invoke(null); + f.ShowDialog(this); + f.Dispose(); + f = null; } catch (Exception expt) { @@ -146,8 +120,16 @@ namespace Examples expt.InnerException ), expt.Message); + + throw; } } + + GC.Collect(); + GC.WaitForPendingFinalizers(); + GC.Collect(); + + this.Visible = true; } } @@ -170,5 +152,43 @@ namespace Examples { RunExample(); } + + /// + /// The main entry point for the application. + /// + [STAThread] + static void Main() + { + try + { + if (File.Exists("debug.log")) + File.Delete("debug.log"); + } + catch (Exception e) + { + MessageBox.Show("Could not access debug.log", e.ToString()); + } + + Debug.Listeners.Clear(); + Debug.Listeners.Add(new TextWriterTraceListener("debug.log")); + Debug.Listeners.Add(new ConsoleTraceListener()); + Debug.AutoFlush = true; + + try + { + using (Form exampleLauncher = new ExampleLauncher()) + { + Application.EnableVisualStyles(); + Application.Run(exampleLauncher); + } + } + finally + { + Debug.Flush(); + Debug.Close(); + Trace.Flush(); + Trace.Close(); + } + } } } diff --git a/Source/Examples/IExample.cs b/Source/Examples/IExample.cs index a1c36e0a..92ab0bbf 100644 --- a/Source/Examples/IExample.cs +++ b/Source/Examples/IExample.cs @@ -11,8 +11,8 @@ using System.Text; namespace Examples { /// - /// Examples should implement this (empty) interface, so that the ExampleLauncher - /// can identify them. + /// This interface is is used by the ExampleLauncher to identify OpenTK examples, + /// your applications do not need to implement or use it. /// interface IExample {