c# - Setting MdiParent property of a form breaks/prevents its Shown event from firing -
so, i've been searching trough similar topics on stackoverflow , on other internet forums , knowledge bases far had no luck trying solve problem i've been struggling whole week. here code:
private void matrículastoolstripmenuitem_click(object sender, eventargs e) { form1 form1 = new form1(); form1.show(); form1.mdiparent = this; // == main form of aplication, wich has ismdiparent property set true. }
if take out "form1.mdiparent = this", shown event of form1 fire normally, executing of handler's content, if let there, shown event of form1 not fire (i did set breakpoits, none of them triggered).
curiously, if use load event instead of shown, works fine, i'm bit afraid if swapping shown load break :(.
try code
form1 form1 = new form1(); //subscribe event here form1.mdiparent = this; form1.show();
this works me
i don't know why code doesn't work, i'll once answer.
edit: have got answer now.
isynchronizationinvoke's
members (invoke
, begininvoke
) implemented control
class follows.
- gets context of thread created window.
- generate new window messageid using
registerwindowmessage
- encapsulate delegate passed parameter in
threadmethodentry
add control's internalqueue
- posts message thread's queue messageid returned
registerwindowmessage
usingpostmessage
- handles
wndproc
listensmessageid
de-queuesthreadmethodentry
, invokes delegate.
what goes wrong here?
form1 form1 = new form1(); form1.show(); form1.mdiparent = this;
form.show
somehow results in call onload
method, onshown
called asynchronously using begininvoke
if (base.ishandlecreated) { base.begininvoke(new methodinvoker(this.callshownevent));//reflected code }
so before posted windowmessage
receives set form1.mdiparent = this;
in turn forces control destroy
it's handle , recreate
new handle.
destroyhandle
method swallows posted message getting using peekmessage
function, , enumerates elements in queue
, sets state completed without invoking delegate marking throw objectdisposedexception
.
form1 form1 = new form1(); form1.show(); action del = () => { console.writeline("this never called");//our custom delegates fails invoked }; var res = form1.begininvoke(del); //after more code form1.endinvoke(res);//throws `objectdisposedexception` marked form1.mdiparent = this;
throwing objectdisposedexception("control")
misleading isn't it?
note: can fix using application.doevents();
before form1.mdiparent = this;
since doevents
process pending messages immediately.
Comments
Post a Comment