mirror of
https://github.com/Ryujinx/Opentk.git
synced 2025-06-18 11:10:22 +00:00
Use separate process for executing examples
Using a separate, isolated process protects against incompatible interactions between the Example Browser (WinForms) and the executing example (native or SDL). It also protects the main GUI from crashes in the example code.
This commit is contained in:
parent
31f2df094b
commit
f8d9667653
|
@ -358,14 +358,6 @@ namespace Examples
|
||||||
if (e == null)
|
if (e == null)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
MethodInfo main =
|
|
||||||
e.Example.GetMethod("Main", BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic) ??
|
|
||||||
e.Example.GetMethod("Main", BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic, null, new Type[] {
|
|
||||||
typeof(object),
|
|
||||||
typeof(object)
|
|
||||||
}, null);
|
|
||||||
if (main != null)
|
|
||||||
{
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
if (parent != null)
|
if (parent != null)
|
||||||
|
@ -376,80 +368,32 @@ namespace Examples
|
||||||
Trace.WriteLine(String.Format("Launching sample: \"{0}\"", e.Attribute.Title));
|
Trace.WriteLine(String.Format("Launching sample: \"{0}\"", e.Attribute.Title));
|
||||||
Trace.WriteLine(String.Empty);
|
Trace.WriteLine(String.Empty);
|
||||||
|
|
||||||
AppDomain sandbox = AppDomain.CreateDomain("Sandbox");
|
var info = new ProcessStartInfo
|
||||||
sandbox.DomainUnload += HandleSandboxDomainUnload;
|
{
|
||||||
|
FileName = Application.ExecutablePath,//.Replace("vshost.exe", String.Empty),
|
||||||
SampleRunner runner = new SampleRunner(main);
|
Arguments = e.Example.ToString()
|
||||||
CrossAppDomainDelegate cross = new CrossAppDomainDelegate(runner.Invoke);
|
};
|
||||||
sandbox.DoCallBack(cross);
|
var process = Process.Start(info);
|
||||||
AppDomain.Unload(sandbox);
|
process.WaitForExit();
|
||||||
}
|
}
|
||||||
finally
|
finally
|
||||||
{
|
{
|
||||||
if (parent != null)
|
if (parent != null)
|
||||||
|
{
|
||||||
|
try
|
||||||
{
|
{
|
||||||
textBoxOutput.Text = File.ReadAllText("debug.log");
|
textBoxOutput.Text = File.ReadAllText("debug.log");
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
Debug.Print(ex.ToString());
|
||||||
|
}
|
||||||
parent.Visible = true;
|
parent.Visible = true;
|
||||||
|
parent.Focus();
|
||||||
Application.DoEvents();
|
Application.DoEvents();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
MessageBox.Show("The selected example does not define a Main method", "Entry point not found", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
static void HandleSandboxDomainUnload(object sender, EventArgs e)
|
|
||||||
{
|
|
||||||
AppDomain sandbox = (AppDomain)sender;
|
|
||||||
sandbox.DomainUnload -= HandleSandboxDomainUnload;
|
|
||||||
}
|
|
||||||
|
|
||||||
[Serializable]
|
|
||||||
class SampleRunner
|
|
||||||
{
|
|
||||||
MethodInfo _main;
|
|
||||||
|
|
||||||
public SampleRunner(MethodInfo main)
|
|
||||||
{
|
|
||||||
_main = main;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void Invoke()
|
|
||||||
{
|
|
||||||
try
|
|
||||||
{
|
|
||||||
using (TextWriterTraceListener dbg = new TextWriterTraceListener("debug.log"))
|
|
||||||
{
|
|
||||||
Trace.Listeners.Add(dbg);
|
|
||||||
Trace.Listeners.Add(new ConsoleTraceListener());
|
|
||||||
|
|
||||||
using (OpenTK.Toolkit.Init())
|
|
||||||
{
|
|
||||||
_main.Invoke(null, null);
|
|
||||||
}
|
|
||||||
|
|
||||||
dbg.Flush();
|
|
||||||
dbg.Close();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
catch (TargetInvocationException expt)
|
|
||||||
{
|
|
||||||
string ex_info;
|
|
||||||
if (expt.InnerException != null)
|
|
||||||
ex_info = expt.InnerException.ToString();
|
|
||||||
else
|
|
||||||
ex_info = expt.ToString();
|
|
||||||
//MessageBox.Show(ex_info, "An OpenTK example encountered an error.", MessageBoxButtons.OK, MessageBoxIcon.Warning);
|
|
||||||
|
|
||||||
Trace.WriteLine(ex_info.ToString());
|
|
||||||
}
|
|
||||||
catch (Exception expt)
|
|
||||||
{
|
|
||||||
Trace.WriteLine(expt.ToString());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Tries to detect the path that contains the source for the examples.
|
// Tries to detect the path that contains the source for the examples.
|
||||||
|
|
|
@ -27,37 +27,49 @@
|
||||||
|
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
using System.Diagnostics;
|
||||||
|
using System.Drawing;
|
||||||
|
using System.IO;
|
||||||
|
using System.Reflection;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using System.Windows.Forms;
|
using System.Windows.Forms;
|
||||||
using System.Diagnostics;
|
|
||||||
using System.IO;
|
|
||||||
using System.Drawing;
|
|
||||||
|
|
||||||
namespace Examples
|
namespace Examples
|
||||||
{
|
{
|
||||||
static class Program
|
static class Program
|
||||||
{
|
{
|
||||||
static void EnableOpenTKHack()
|
static void LaunchExample(string type)
|
||||||
{
|
{
|
||||||
// If OpenTK is not initialized before Windows.Forms,
|
using (TextWriterTraceListener dbg = new TextWriterTraceListener("debug.log"))
|
||||||
// the program will crash on Mac OS X. This hack will
|
|
||||||
// enable OpenTK in a temporary AppDomain before entering
|
|
||||||
// the main program - this appears to be enough.
|
|
||||||
var domain = AppDomain.CreateDomain("sandbox");
|
|
||||||
domain.DoCallBack(() => {
|
|
||||||
using (OpenTK.Toolkit.Init())
|
using (OpenTK.Toolkit.Init())
|
||||||
{
|
{
|
||||||
|
Trace.Listeners.Add(dbg);
|
||||||
|
Trace.Listeners.Add(new ConsoleTraceListener());
|
||||||
|
|
||||||
|
var example = Type.GetType(type);
|
||||||
|
if (example != null)
|
||||||
|
{
|
||||||
|
example.InvokeMember("Main",
|
||||||
|
BindingFlags.InvokeMethod | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic,
|
||||||
|
null, null, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
dbg.Flush();
|
||||||
|
dbg.Close();
|
||||||
}
|
}
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
[STAThread]
|
[STAThread]
|
||||||
public static void Main()
|
public static void Main(string[] args)
|
||||||
|
{
|
||||||
|
if (args.Length > 0)
|
||||||
|
{
|
||||||
|
LaunchExample(args[0]);
|
||||||
|
}
|
||||||
|
else
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
EnableOpenTKHack();
|
|
||||||
|
|
||||||
Application.EnableVisualStyles();
|
Application.EnableVisualStyles();
|
||||||
Application.SetCompatibleTextRenderingDefault(false);
|
Application.SetCompatibleTextRenderingDefault(false);
|
||||||
|
|
||||||
|
@ -86,4 +98,5 @@ namespace Examples
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue