3

SharpDX has a RenderLoop that runs a given delegate in the render loop:

RenderLoop.Run(m_RenderForm, () =>
{
    // Do stuff here to render things...
}

What I need to do is exit the render loop somehow.

RenderLoop.Run(m_RenderForm, () =>
{
    if (DoINeedToQuit() == true)
    {
        // What do I put here?
    }
}

I can't just return, because that only ends the current loop iteration.

Davide Cannizzo
  • 2,826
  • 1
  • 29
  • 31
Cylindric
  • 5,858
  • 5
  • 46
  • 68

5 Answers5

0

You can dispose it:

myDXControl.Dispose();

Disposing the control causes the render loop to stop. In order to restart the loop, make sure it is disposed myDXControl.IsDisposed, then reinitialize the control and start the loop over again.

Davide Cannizzo
  • 2,826
  • 1
  • 29
  • 31
smoothumut
  • 3,423
  • 1
  • 25
  • 35
0

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;
}
Davide Cannizzo
  • 2,826
  • 1
  • 29
  • 31
-1

You'd have to exit the application (Application.Exit).

Alternatively, you can do the job you want to do outside of the loop, inside the loop.

Davide Cannizzo
  • 2,826
  • 1
  • 29
  • 31
cede
  • 22
  • 2
-1

A possible solution is to destroy the control the RenderLoop is rendering onto.

For example,

RenderForm.Close();
Davide Cannizzo
  • 2,826
  • 1
  • 29
  • 31
Croll
  • 3,631
  • 6
  • 30
  • 63
-2

As one of the initial posters has shown, the code is fairly simple within the loop. The function you have called is for convenience, you could actually just role your own in the form. Don't need to recompile libraries, just add a function to your form if application.exit is not the only exit trigger you want.

ErnieDingo
  • 444
  • 5
  • 11