Except that this example is only half the logic that is required to continuously fetch sim vars. I assume you took it from here, too (like I did, and most other developers as well):
The code we‘re all interested in is here:
// Sections of code in DispatchProc
Yes, that‘s right. The examples hides the most crucial parts with „…“, namely how to register our callback, and how to make SimConnect call it automatically.
Well, it seems we have to help SimConnnect a little to „remember“ to call our callback. Enter all the examples and tutorials that use… a „polling loop“!
E.g. the above API doc leads us to a working example, like „Tagged Data“:
Lo and behold, here is our „polling loop“:
while( 0 == quit )
SimConnect_CallDispatch(hSimConnect, MyDispatchProcPDR, NULL);
Yes, that‘s right. Unless we call „CallDispatch“ ourselves our callback won‘t be called. So we use either a loop with some „sleep“ interval, or a timer. Polling at its best.
Here is another example, „Request Data“ (I believe that is the one from which the above example code with the „kohlsmann“ variable as taken):
And again the same polling loop which repeatedly calls CallDispatch.
Now there is an interesting addendum in the API docs given for this CallDispatch:
But first of all:
„ It is important to call this function sufficiently frequently that the queue of information received from the server is processed (typically it is coded within a while loop that terminates when the application is exited).“
So this seems to confirm that „polling“ is the intended usage of this SimConnect API (despite a callback!).
But here comes the interesting part:
„ However, if the project involves developing a library (DLL) rather than an application (EXE) then only one call to this function is necessary. This call will store the name of the callback in a cache, and whenever a packet is sent to the client, the callback function will be run“
So if you develop your SimConnect logic in a DLL there seems to be a way to register your callback such that it is actually called whenever messages arrive.
The problem: you need to connect (to the flight simulator) and register your callback in int __stdcall DLLStart(void), which is called when your DLL is loaded (started). Or in other words: the flight simulator must already be running (ready to accept connections) at that point in time.
And whether that DLL voodoo magic also works with the SimConnect implementation of FS 2020 is yet another question… (the doc refers to Prepar3D).
So if anyone has a recipe / design pattern to avoid „polling loops“ and timers… I am curious to know