• Which the release of FS2020 we see an explosition of activity on the forun and of course we are very happy to see this. But having all questions about FS2020 in one forum becomes a bit messy. So therefore we would like to ask you all to use the following guidelines when posting your questions:

    • Tag FS2020 specific questions with the MSFS2020 tag.
    • Questions about making 3D assets can be posted in the 3D asset design forum. Either post them in the subforum of the modelling tool you use or in the general forum if they are general.
    • Questions about aircraft design can be posted in the Aircraft design forum
    • Questions about airport design can be posted in the FS2020 airport design forum. Once airport development tools have been updated for FS2020 you can post tool speciifc questions in the subforums of those tools as well of course.
    • Questions about terrain design can be posted in the FS2020 terrain design forum.
    • Questions about SimConnect can be posted in the SimConnect forum.

    Any other question that is not specific to an aspect of development or tool can be posted in the General chat forum.

    By following these guidelines we make sure that the forums remain easy to read for everybody and also that the right people can find your post to answer it.

FSX SimConnect vs. Windows message loop

KWB

Messages
69
Country
germany
I have a console application with SimConnect, this gives me the first
message loop / callback combination:

Code:
while( 0 == quit ) {
 SimConnect_CallDispatch(hSimConnect, MyDispatchProcMI, NULL);
 Sleep(50);
}

Within my application I create a window planned to serve as a child window in FSX.
Here I have a second message loop:

Code:
MSG msg;
while (GetMessage(&msg, NULL, 0, 0)) {
  TranslateMessage(&msg);  
  DispatchMessage(&msg);
}

Since I hardly can have two loops, I see the following options:

  1. One loop runs in an independent thread, which is at least not possible for window message loops.
  2. The SimConnect handling goes in the Windows Message loop
  3. Vice versa, the DispatchMessage for the window goes in the SimConnect loop

Which is the right way to proceed? And hints, suggestions?


Updated: I can merge them both in the SimConnect loop, this works. But is the way to go?

Code:
MSG msg;
while (0 == quit)  {
	if (hSimWindowInclude && PeekMessage(&msg, hSimWindowInclude, 0, 0, PM_REMOVE)) {
		TranslateMessage(&msg);
		DispatchMessage(&msg);
	}
	SimConnect_CallDispatch(hSimConnect, MyDispatchProcMI, NULL);
	Sleep(50);
}
 
Last edited:
It is possible to run a Windows message loop in an own thread. I do that particularly for OGL or DirectX windows to protect the graphics from unnecessary system messages. Even the SimConnect loop runs in an own threat. But you have to take care for some process specific rules. Especially when you try to use SimConnect out of another thread. Or using Windows DCs out of another thread.
Regards
Mike
 
An amendment to my previous answer (I should have red the link before):
What they describe is a mismatch of the procedure how a message loop works. Every windows runs in thread. If you want to get it running in an own thread, you have to do something like this:

Code:
void A10_AUX_WIN::AUXwindowPump(void)
{
	MSG msg;
	WNDCLASS wc;
	HINSTANCE hInstance;
	hInstance = GetModuleHandle(NULL);
	wc.style = CS_HREDRAW | CS_VREDRAW;
	wc.lpfnWndProc = (WNDPROC) ClassQueue;
	wc.cbClsExtra = 0;
	wc.cbWndExtra = 0;
	wc.hInstance = hInstance;
	wc.hIcon = LoadIcon(hInstance,IDI_APPLICATION);
	wc.hCursor = LoadCursor(NULL, IDC_ARROW);
	wc.hbrBackground = (HBRUSH)GetStockObject(NULL_BRUSH);
	wc.lpszMenuName = NULL;
	wc.lpszClassName = szAppNam;
	RegisterClass(&wc);

	AppRep->Protocoll(1,"Registered Window class");

	thisWindow = CreateWindowEx(	0,szAppNam,
		"",
		WS_OVERLAPPEDWINDOW | WS_VISIBLE,
		iniX, iniY, iniWidth, iniHight,
		NULL,
		NULL,
		hInstance,
		NULL );
	while(GetMessage(&msg,thisWindow,0,0))
	{
		TranslateMessage(&msg);
		DispatchMessage(&msg);
	}
	AppRep->Protocoll(1,"Thread ended...");
	CloseHandle(thrHdl);
	thrHdl = NULL;

}
AUXwindowPump is a thread body that serves a DirectX window in a DLL. As you can see in the message pump loop, I use the handle of the newly created window in GetMessage. I think that is the difference to your description. The window can be addressed like every other window, with SendMessage or PostMessage.
Regards
Mike
 
Last edited:
Thanks / SimConnect in independent thread

Thanks for the nice example TOWSIM, great stuff. ;)

You've mentioned some prerequisites about running SimConnect in an own thread. What are they, is there some kind of documentation about running SimConnect in an independent thread or have you figured this out own your own?
 
I take special care, that all handling routines called out of the dispatcher and even the calls to a SimConnect function are in the same thread. I made the experience, that some SimConnect functions fail, if you call them from a different thread. Whereby, if you are called from SimConnect via the dispatcher, you are in a different thread. In this special case it is possible to call the interface. Different thread means, a thread which did not open the SimConnect interface. The real advantage calling SimmConnect out of a thread is, you have all control over the timing and you are not disturbed by other functions. Unfortunately there is no description for these trap doors. May be, I didn't find one up to now.
Regards
Mike
 
Last edited:
Thanks a lot again for your time and effort! I am just beginning with this stuff, so pointing out the right path surely saves me a lot of time!
 
Back
Top