Application.restart - Puzzling behaviour in VB.Net

后端 未结 3 363
野趣味
野趣味 2020-12-20 20:20

OK guys, what\'s going on here? In this VB code:

Module Module1
Sub Main()


    If MsgBox(\"Restart?\", MsgBoxStyle.OkCancel) = MsgBoxResult.Ok Then
                


        
3条回答
  •  悲&欢浪女
    2020-12-20 21:15

    This is going to be, admittedly, a bit of a guess based on some fairly top-level reading I've done about Application.Restart(), but I think this is occurring due to the way Restart operates internally.

    I think Restart() tries to do as much "intelligent" cleanup as it can for a process that is being terminated, and in what may be considered a fairly simplistic implementation, tracks certain of the things to be "cleaned up," possibly calling Dispose() on them (if applicable), which normally is a reasonable step to take. In your case, I'm going to make the guess that a background thread, or form, holds a reference to something - can't say what - that prevents the code from shutting down. It may become aware that it is executing inside a method, and wants to give that method a chance to complete before killing it - waiting on the completion of that sub/method.

    I've seen other instances of Restart actually causing a really strange "Collection was Modified" error when no collection was involved. That's suggesting to me, probably naively, that the internal cleanup Restart is trying to achieve is reposed in a simple list, but in certain circumstances, the cleanup modifies the element in an unexpected way, a way that modifies the collection, causes the exception to be thrown, and aborts the exit/restart.

提交回复
热议问题