# Custom event loop for generic render window interactor

**URL:** https://discourse.vtk.org/t/custom-event-loop-for-generic-render-window-interactor/7415
**Category:** Support
**Created:** [December 16, 2021, 12:36pm UTC](https://discourse.vtk.org/t/custom-event-loop-for-generic-render-window-interactor/7415 "2021-12-16T12:36:14Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![brnk](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/b/ecd19e/32.png) [@brnk](https://discourse.vtk.org/u/brnk)
#### Post date: [December 16, 2021, 12:36pm UTC](https://discourse.vtk.org/t/custom-event-loop-for-generic-render-window-interactor/7415/1 "2021-12-16T12:36:14Z")

</div>

Hello, I want to implement my own event loop using a `vtkGenericRenderWindowInteractor` instance. I couldn’t find any examples where the event loop is implemented.

The code should have the following structure:

```auto
#include <vtkGenericRenderWindowInteractor.h>

vtkNew<vtkGenericRenderWindowInteractor> interactor;

// custom event
int renderEvent = vtkCommand::UserEvent + 1;

function processData()
{
    // do some data processing

    // when finished, update the visualization
    interactor->InvokeEvent(renderEvent); // invoke custom event to render the scene
}

int main()
{
    // initialize the visualization
    // ...

    // Start the interactor without blocking the execution
    interactor->Start();

    // Event loop
    while (true)
    {
        processData(); // do some data processing and update the scene when finished

        // invoke mouse events for interactions with the scene
        // ...
    }
}

```

However, I have several questions.

First, if the execution of `processData()` takes 1 second, would the interactor be unresponsive during that time? If yes, can this be solved by calling `processData()` in a separate thread with a flag that checks if the previous call was finished before `processData()` is called again?

Second, how can I enable the same events of a `vtkRenderWindowInteractor` instance in a `vtkGenericRenderWindowInteractor` instance, i.e., the mouse events such as rotating, etc.?

---

<div class="post-metadata">

### Author: ![Andrew](https://discourse.vtk.org/user_avatar/discourse.vtk.org/andrew/32/3861_2.png) [@Andrew](https://discourse.vtk.org/u/Andrew)
#### Post date: [December 17, 2021, 2:23am UTC](https://discourse.vtk.org/t/custom-event-loop-for-generic-render-window-interactor/7415/2 "2021-12-17T02:23:06Z")

</div>

This may be done with the following code:

```auto
interactor->Initialize();
while (running)
 {
    interactor->ProcessEvents();
}
renderWindow->Finalize();
renderWindow->GetInteractor()->TerminateApp();

```

Useful if you need to nest VTK in your own event system.

The vtkOpenVRInteractor initialization is slightly different.

Also note ProcessEvents is broken for Linux on VTK 9.0. It is fixed in VTK 9.1 though.

---

<div class="post-metadata">

### Author: ![brnk](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/b/ecd19e/32.png) [@brnk](https://discourse.vtk.org/u/brnk)
#### Post date: [December 17, 2021, 11:49am UTC](https://discourse.vtk.org/t/custom-event-loop-for-generic-render-window-interactor/7415/3 "2021-12-17T11:49:22Z")

</div>

Thank you for your response. Unfortunately, I forgot to mention that I’m still using VTK 8. It seems like `ProcessEvents()` is not available in VTK 8. Is there a workaround or an alternative method?

---

<div class="post-metadata">

### Author: ![Andrew](https://discourse.vtk.org/user_avatar/discourse.vtk.org/andrew/32/3861_2.png) [@Andrew](https://discourse.vtk.org/u/Andrew)
#### Post date: [December 17, 2021, 8:38pm UTC](https://discourse.vtk.org/t/custom-event-loop-for-generic-render-window-interactor/7415/4 "2021-12-17T20:38:58Z")

</div>

Ah yeah. Anyways, it should still use an event loop whether it exposes it or not. So what you can do is depending on the system you’re targeting, or both. Copy vtkWin32RenderWindowInteractor or vtkXRenderWindowInteractor. Rename them to vtkWin32RenderWindowInteractor2 or something & use it instead.

If we pop open vtkWin32RenderWindowInteractor we can see,

[https://gitlab.kitware.com/vtk/vtk/-/blob/v8.2.0/Rendering/OpenGL2/vtkWin32RenderWindowInteractor.cxx#L146](https://gitlab.kitware.com/vtk/vtk/-/blob/v8.2.0/Rendering/OpenGL2/vtkWin32RenderWindowInteractor.cxx#L146)

Which implements said event loop. Handling win32 system messages.

Similar linux will be done here: [https://gitlab.kitware.com/vtk/vtk/-/blob/v8.2.0/Rendering/OpenGL2/vtkXRenderWindowInteractor.cxx#L249](https://gitlab.kitware.com/vtk/vtk/-/blob/v8.2.0/Rendering/OpenGL2/vtkXRenderWindowInteractor.cxx#L249)

---

<div class="post-metadata">

### Author: ![Andrew](https://discourse.vtk.org/user_avatar/discourse.vtk.org/andrew/32/3861_2.png) [@Andrew](https://discourse.vtk.org/u/Andrew)
#### Post date: [December 17, 2021, 8:40pm UTC](https://discourse.vtk.org/t/custom-event-loop-for-generic-render-window-interactor/7415/5 "2021-12-17T20:40:50Z")

</div>

It’s also worth mentioning mentioning whatever data you are wanting to process. You could setup a timer event which would be invoked by VTK every Nms (outside of rendering, ie: done in sequence).

[https://kitware.github.io/vtk-examples/site/Cxx/Utilities/Timer/](https://kitware.github.io/vtk-examples/site/Cxx/Utilities/Timer/)
