在创建窗口句柄之前,不能在控件上调用 Invoke 或 BeginInvoke 及This.DialogResult

在创建窗口句柄之前,不能在控件上调用 Invoke 或 BeginInvoke 及This.DialogResult

在创建窗口句柄之前,不能在控件上调用 Invoke 或 BeginInvoke

 

昨天改了个窗体代码,今天运行,突然发现报出这么个错误.

 

又测试时发现运行时窗体会自动消失,我没加关闭窗体代码,怎么会关闭呢.

查资料,说控件在没建立的时候,调用invoke/beginInvoke 就会出这个错误,仔细看代码,发现我加过一句话:

This.DialogResult = DialogResult .Ok执行完这句话,窗体就自动关了...

 

下面是找的资料:

Remarks

The dialog result of a form is the value that is returned from the form when it is displayed as a modal dialog box. If the form is displayed as a dialog box, setting this property with a value from the DialogResult enumeration sets the value of the dialog box result for the form, hides the modal dialog box, and returns control to the calling form. This property is typically set by the DialogResult property of a Button control on the form. When the user clicks the Button control, the value assigned to the DialogResult property of the Button is assigned to the DialogResult property of the form.

When a form is displayed as a modal dialog box, clicking the Close button (the button with an X in the top-right corner of the form) causes the form to be hidden and the DialogResult property to be set to DialogResult.Cancel. The Close method is not automatically called when the user clicks the Close button of a dialog box or sets the value of the DialogResult property. Instead, the form is hidden and can be shown again without creating a new instance of the dialog box. Because of this behavior, you must call the Dispose method of the form when the form is no longer needed by your application.

You can use this property to determine how a dialog box is closed in order to properly process the actions performed in the dialog box.

 

原来这个属性还有这种功能,以前用倒是疏忽了.

 

When a form is closed, all resources created within the object are closed and the form is disposed. You can prevent the closing of a form at run time by handling the Closing event and setting the Cancel property of the CancelEventArgs passed as a parameter to your event handler. If the form you are closing is the startup form of your application, your application ends.

The two conditions when a form is not disposed on Close is when (1) it is part of a multiple-document interface (MDI) application, and the form is not visible; and (2) you have displayed the form using ShowDialog. In these cases, you will need to call Dispose manually to mark all of the form's controls for garbage collection.

 

原来用了showDialog()显示的窗体在关闭(close())时,是关闭(隐藏)窗体但不自动释放(Dispose)的,怪不得看不到窗口,代码却仍在运行.

posted on 2008-09-03 16:45  文强 阅读( ...) 评论( ...) 编辑 收藏

转载于:https://www.cnblogs.com/Gerry-BJ/archive/2008/09/03/1283155.html

你可能感兴趣的:(在创建窗口句柄之前,不能在控件上调用 Invoke 或 BeginInvoke 及This.DialogResult)