Since I faced this problem as well, I've taken a look at the source code of SharpDX and have found a solution.
Below is the source code of the Run
method:
public static void Run(Control form, RenderCallback renderCallback, bool useApplicationDoEvents = false)
{
if (form == null)
throw new ArgumentNullException("form");
if (renderCallback == null)
throw new ArgumentNullException("renderCallback");
form.Show();
using (var renderLoop = new RenderLoop(form) { UseApplicationDoEvents = useApplicationDoEvents })
while (renderLoop.NextFrame())
renderCallback();
}
In the while
there's a condition to continue; it'd be enough to modify that condition. You may want to create a static class with the following code:
private static bool mExitLoop = false;
public static void Run(Control form, RenderCallback renderCallback, bool useApplicationDoEvents = false)
{
if (form is null)
throw new ArgumentNullException(nameof(form));
if (renderCallback is null)
throw new ArgumentNullException(nameof(renderCallback));
Contract.EndContractBlock();
form.Show();
using (var renderLoop = new RenderLoop(form) { UseApplicationDoEvents = useApplicationDoEvents })
{
while (renderLoop.NextFrame() && !mExitLoop)
renderCallback();
}
mExitLoop = false;
}
public static void ExitLoop()
{
mExitLoop = true;
}