[英]How can I examine the CancelEventArgs from Application.Exit()?
In my WinForms app, I call Application.Exit()
in certain circumstances. 在某些情况下,我在WinForms应用程序中调用Application.Exit()
。
Application.Exit()
has an overload that looks like this: Application.Exit()
具有如下所示的重载:
Application.Exit(CancelEventArgs e)
And the documentation says that e
"Returns whether any Form within the application cancelled the exit." 并且文档说e
“返回应用程序内的任何表单是否取消了退出。”
However, it's unclear to me how I could ever examine e
. 但是,我不清楚如何检查e
。 The method returns void, and e
is not defined as an out variable. 该方法返回void,并且e
未定义为out变量。 Am I supposed to be able to examine this? 我应该可以检查一下吗?
Yes, I did look at other questions regarding Application.Exit(), but none of them address this. 是的,我确实查看了有关Application.Exit()的其他问题,但是这些问题都没有解决。 They address handling the event, not calling the method. 他们处理事件,而不是调用方法。
Instantiate a CancelEventArgs
variable and test its Cancel
property after call to the Application.Exit
: 在调用Application.Exit
之后,实例化CancelEventArgs
变量并测试其Cancel
属性:
CancelEventArgs e = new CancelEventArgs();
Application.Exit(e);
if (e.Cancel)
{
// Cancelled
}
The reason you can examine e
is that e
is a reference to the CancelEventArgs
object you pass in the method call. 您可以检查e
的原因是e
是对您在方法调用中传递的CancelEventArgs
对象的引用。 A variable of a reference type does not contain its data directly; 引用类型的变量不直接包含其数据。 it contains a reference to its data. 它包含对其数据的引用。 When you pass a reference-type parameter , in this case a reference to a CancelEventArgs
object, the called method is able to use the reference to access properties of the CancelEventArgs
object, and it may for example set e.Cancel
to true
. 当您传递引用类型参数 (在这种情况下为对CancelEventArgs
对象的引用)时,被调用的方法能够使用该引用来访问CancelEventArgs
对象的属性,例如,可以将e.Cancel
设置为true
。
After the method call completes, the story is the same: e
is still a reference to the CancelEventArgs
object you passed, and you may now examine its properties to establish whether any have been changed by the called method. 在方法调用完成之后,情况是一样的: e
仍然是对您传递的CancelEventArgs
对象的引用,现在您可以检查其属性以确定被调用方法是否更改了任何属性。
EDIT I see from your comment it's not yet clear, so consider this: passing e
as ref
would mean the called method could change e
to refer to a different CancelEventArgs
object. 编辑我从您的评论中看到还不清楚,所以考虑一下:将e
传递为ref
意味着被调用的方法可以将e
更改为引用不同的CancelEventArgs
对象。 It has nothing to do with whether it can set properties in the existing CancelEventArgs
object. 它是否可以在现有的CancelEventArgs
对象中设置属性与它无关。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.