由于我也遇到了这个问题,所以我查看了 SharpDX 的源代码并找到了解决方案。
下面是该Run
方法的源代码:
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();
}
在while
有条件的情况下继续;修改那个条件就足够了。您可能希望使用以下代码创建一个静态类:
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;
}