Threading in WFC Controls

The Java language supports multithreading through a set of classes and APIs to control thread creation and execution. Win32 windows are inherently apartment threaded. That is, they can be created on any thread, but they cannot switch threads, and all function calls to that window must occur on its main thread. WFC controls (anything derived from com.ms.wfc.ui.Control) require this threading model, while most WFC components are free threaded.

The Control class provides two functions, invoke and invokeAsync, that will marshal any delegate onto the control’s main window thread and execute it. If you attempt to call a function on a control that manipulates the underlying Win32 HWND, it will get marshaled to the correct thread by Windows. By calling the invoke function directly, you can batch up calls to the HWND and limit the number of expensive cross thread marshals that occur.

Note   The Windows message loop is tied to the thread that the HWND was created on. If the control's HWND is recreated on a new thread, the new thread won't receive messages any more. For more details, see Using Java Threads with WFC.

Although most WFC components support free threading, most of the method calls are not synchronized. Synchronization is very expensive and should be done only when needed. For example, by synchronizing on the object and calling multiple methods, you will get substantial performance benefits.