private void WaitForPreviousFrame()
{
// WAITING FOR THE FRAME TO COMPLETE BEFORE CONTINUING IS NOT BEST PRACTICE.
// This is code implemented as such for simplicity.
int currentFence = fenceValue;
commandQueue.Signal(fence, currentFence);
fenceValue++;
// Wait until the previous frame is finished.
if (fence.CompletedValue < currentFence)
{
fence.SetEventOnCompletion(currentFence, fenceEvent.SafeWaitHandle.DangerousGetHandle());
fenceEvent.WaitOne();
}
frameIndex = swapChain.CurrentBackBufferIndex;
}