Question

I'm not sure if this is really a SharpDX question, or a general C# question.

SharpDX has a render loop which runs with the use of a delegate:

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

So 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 just ends the current loop, and starts again at the beginning.

Was it helpful?

Solution

You'd have to exit the application (Application.Exit). The other option is, to do the job you want to to outside of the loop, inside the loop.

OTHER TIPS

You can dispose it .

myDXControl.Dispose();

then loop stops. in order to restart the loop, you can check if it is disposed myDXControl.IsDisposed then you can initialize the control again and start the loop. hope it helps

Other solution is to destroy control "in which" renderLoop is working, for example, do RenderForm.Close()

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.

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;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top