Is it safe to call close on a form and then call close again on it. no other operations are being done with it after the first close.
my suspicion is that this is bad and it is effectively racing the GC to call the second close? (and winning 99.999% of the time so I'm not seeing any error)
Edit: to be clear this is IMHO a bug, but it is not currently causing any exception to be thrown. the problem is more how big an issue 开发者_C百科is it? should I fix the one I've found and move on because it's relatively benign, or should I review every form in the design to make sure there isn't any similar issue on the grounds that the lack of exception may depend on GC behaviour.
The second call isn't going to close the native window anymore, it's gone. It will just call Dispose() again. Which is fine, disposing an object multiple times is supported in all .NET classes.
None of this has anything to do with the garbage collector, clearly you still have a reference to the form object or you wouldn't have been able to call Close(). Don't keep this reference around forever or you'll leak the form object. It is otherwise dead and can't be revived. Referencing members of that form object tends to throw ObjectDisposedException.
It should be fixed, but it might not be a big priority. The problem is that it MAY cause exceptions or odd behavior in the future, after modifications to the application or when a newer version of .net is used. In other words, it's not safe.
Calling Close()
on a form that's already closed suggests some design issues you should rethink. However, I'm not aware of any problems this causes.
You could use Reflector to see exactly what close does. But I'm pretty sure it checks if the form is actually open.
There is no sure way to know what the GC is doing. If you have a form and call Close()
on it twice and there is not an exception thrown (either Null or Disposed), then where is the problem? There would not be any 'racing' condition.
My question would be, "why are there 2 calls to Close() in the first place?"
精彩评论