Multithread Error When Using a Dialog Box in LabWindows™/CVI

Updated Jul 26, 2023

Reported In

Software

  • LabWindows/CVI

Issue Details

  • I am trying to run the FileSelectPopupEx function in LabWindows™/CVI and I am getting the following error:
NON-FATAL RUN-TIME ERROR: "d:\...\Code_C\Emco_Exp_V7 - CVI2020\EXP.C", line 9077, col 12, thread id 119520: Function FileSelectPopupExAnsi: (return value == -180 [0xffffff4c]). The dialog cannot be displayed in a thread whose concurrency model is multithread apartment (MTA)

Solution

As mentioned in the help files of FileSelectPopupEx, this function is prone to this error when threads are not initialized properly. The underlying Windows SDK function used to create and run the FileSelectPopupEx dialog box does not work properly when the threading model is multithread apartment (MTA). ActiveX functions use this thread type by default, and calling any ActiveX function initializes the thread to MTA. This might cause subsequent calls to the FileSelectPopupEx function to return an error. 

Make sure LabWindows/CVI initializes the thread calling the FileSelectPopupEx function to COINIT_APARTMENTTHREADED prior to creating the dialog box. This will ensure the dialog box works properly,

If you want to run in the default MTA threading model, consider calling the FileSelectPopupEx function from a different thread that does not call any ActiveX functions. You can explicitly set the ActiveX threading model for a thread using the CA_InitActiveXThreadStyleForCurrentThread function.
 

Additional Information

For more information about threading models for Active X applications visit: COM Threading Models for ActiveX Applications .