Поделиться через


VB My.Forms and background threads

If you are using new VB My.Forms feature, and also write a multithread application, you might notice that My.Forms.MyForm1 actually returns different instance in different thread.  So if your background thread tries to using this to update something on the UI, you might have changed a different object.  It sounds a little bit strange, because many background threads are not UI threads, it makes nonsense to create a Form object in that thread.

The design of this is understandable, because the HWND is a thread-boundary resource. If all threads share the same instance, the Form might be created by the first thread accessing this property. It will be very confusing, because all window messages will be processed in that thread, and the window will be destroyed when the thread exits. 

Bascially, we should not use this feature in the background thread unless we know what we are doing.