winforms - C# - Possible to safely have an owned form in a separate thread? -
winforms - C# - Possible to safely have an owned form in a separate thread? -
i attempting write specialized onscreen keyboard (osk) application i'm writing in c#. facilitate this, i've created form has several buttons on representing keys, , clicking them calls sendkeys
, sends out appropriate keys.
this form owned main window shown when application first starts, using owner
property. way, osk pops whenever user focuses application, , stays on top of main window if said main window dragged on it.
this works great, because have modal dialogs want utilize osk, attempted create in separate thread, finish own message loop (via application.run
) still usable modal dialogs in main thread.
the problem that, obviously, beingness in separate thread can cause invalidoperationexception
s because of cross-threaded calls. 1 specific illustration of when calling application.run(osk)
new thread, cross thread error occurs because attempting update window's handle owner (the main window).
my question is, possible have owned form on thread separate owner in safe manner? and, if failing that, possible emulate owned form's characteristics (namely beingness on top main window, , popping when main window focused)?
thanks, , sorry if confusing.
i think bug in windows forms. inevitable due way checks access handle property wrong thread. sdk docs setparent not explicit it, states requirement both windows belong same application. no mention of having belong same thread. know fact 'same application' requirement not hard, there's appcompat code in windows makes work windows different processes. adobe acrobat ab/used long time. absolves 'same thread' requirement.
well, punt problem , seek out. set control.checkforillegalcrossthreadcalls false before set owner, true afterward. , test heck out of it. if have problem seek pinvoking setparent() straight instead of setting owner. windows forms uses setwindowlongptr not recommended sdk.
c# winforms multithreading
Comments
Post a Comment