Results 1 to 8 of 8

Thread: [RESOLVED] Close a child form of MDI Form on Load

  1. #1

    Thread Starter
    King of sapila
    Join Date
    Oct 2006
    Location
    Greece
    Posts
    6,605

    Resolved [RESOLVED] Close a child form of MDI Form on Load

    Hi.
    I'm on a tricky situation here.

    I need to close a child form of MDI, if the form has an error when Loading.
    Apparently this cannot be done inside the Load event of the child form ( as me.close() ).
    I though about closing in the _show event but since the form is async, the event fires before load completes.
    Moving all the code from the Load event is quite difficult.
    So any opinions?

    What I am going to do if I can't figure this out is setting a global parameter on the Mdi form when child completes load. If it is true, do nothing, if false, then close the child, but I'm wondering if there is a solution for fix this strictly on the child form.

    Thanks.
    ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς μάλα πολλὰ
    πλάγχθη, ἐπεὶ Τροίης ἱερὸν πτολίεθρον ἔπερσεν·

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,344

    Re: Close a child form of MDI Form on Load

    This worked for me:
    vb.net Code:
    1. Public Class Form2
    2.  
    3.     Private work As Task
    4.  
    5.     Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    6.         work = DoWorkAsync()
    7.     End Sub
    8.  
    9.     Private Async Sub Form2_Shown(sender As Object, e As EventArgs) Handles Me.Shown
    10.         Try
    11.             Await work
    12.         Catch
    13.             Close()
    14.         End Try
    15.     End Sub
    16.  
    17.     Private Async Function DoWorkAsync() As Task
    18.         Await Task.Delay(TimeSpan.FromSeconds(5))
    19.  
    20.         Throw New Exception("Boom!")
    21.     End Function
    22.  
    23. End Class
    The form was displayed but then closed automatically after 5 seconds. In fact, this also worked:
    vb.net Code:
    1. Public Class Form2
    2.  
    3.     Private Async Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    4.         Try
    5.             Await DoWorkAsync()
    6.         Catch
    7.             Close()
    8.         End Try
    9.     End Sub
    10.  
    11.     Private Async Function DoWorkAsync() As Task
    12.         Await Task.Delay(TimeSpan.FromSeconds(5))
    13.  
    14.         Throw New Exception("Boom!")
    15.     End Function
    16.  
    17. End Class

  3. #3

    Thread Starter
    King of sapila
    Join Date
    Oct 2006
    Location
    Greece
    Posts
    6,605

    Re: Close a child form of MDI Form on Load

    HI JMC.
    Well the Load has async and normal tasks.
    Again the issue only happens on child forms of MDI.
    Maybe the issues is that the Load is not Declared as async but I prefer your first example, I will try to push all of my async code to the _shown part of the form and see if I have any luck doing so.
    Thanks

    Edit:
    The load form was actually declared as async, so the closing calls should have worked but they don't also, moving on _Show did nothing.
    There may also be a problem with controls changing values inside the async. I have a global catch function on the application events but it does not catch anything.

    So as I would be having to take a look at roughly 10k lines of code, I just went to plan B and inserted a value - to be deleted - on the main MDI form that do a light swipe every 5 second and clear any crashed forms.
    Last edited by sapator; Mar 30th, 2021 at 01:10 PM.
    ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς μάλα πολλὰ
    πλάγχθη, ἐπεὶ Τροίης ἱερὸν πτολίεθρον ἔπερσεν·

  4. #4
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,479

    Re: [RESOLVED] Close a child form of MDI Form on Load

    You don't need to be looking to close crashed forms. The correct way is to avoid the crashes properly...

  5. #5

    Thread Starter
    King of sapila
    Join Date
    Oct 2006
    Location
    Greece
    Posts
    6,605

    Re: [RESOLVED] Close a child form of MDI Form on Load

    Yess, but...

    I'm using remote WMI calls on old computers. Sometimes WMI will just crash. That is not my fault and I can't do anything about it, except maybe dump the computers.
    ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς μάλα πολλὰ
    πλάγχθη, ἐπεὶ Τροίης ἱερὸν πτολίεθρον ἔπερσεν·

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,344

    Re: [RESOLVED] Close a child form of MDI Form on Load

    I think "crash" is probably an inaccurate term in this case. A crash is an unhandled exception. I think that what .paul. is suggesting is that you handle the exceptions appropriately, but I expect that that is already what you're doing and you want to close the form when you catch such an exception. Throwing an exception is not crashing. Failing to catch an exception that is thrown is crashing.

  7. #7

    Thread Starter
    King of sapila
    Join Date
    Oct 2006
    Location
    Greece
    Posts
    6,605

    Re: [RESOLVED] Close a child form of MDI Form on Load

    Yes, correct.
    Unfortunately there is not much I can do when WMI crashes but just catch it.
    Also I tried using something similar to this code on apllicationevent so I maybe could catch the exception there and close the from from this class but I does not really catch any exception at all.
    Maybe I haven't written something correctly but, for now a timer seems to work:

    If you're interested to take a look:
    Code:
      Private Delegate Sub SafeApplicationThreadException(ByVal sender As Object,
            ByVal e As Threading.ThreadExceptionEventArgs)
    
            Private Sub ShowDebugOutput(ByVal ex As Exception)
    
                'Display the output form
    
    
                'Perform application cleanup
                'TODO: Add your application cleanup code here.
    
                'Exit the application - Or try to recover from the exception:
               'Form close attempt... or
               ' Environment.Exit(0)
    
            End Sub
    
            Private Sub app_ThreadException(ByVal sender As Object,
        ByVal e As Threading.ThreadExceptionEventArgs)
    
                'This is not thread safe, so make it thread safe.
                If MainForm.InvokeRequired Then
                    ' Invoke back to the main thread
                    MainForm.Invoke(New SafeApplicationThreadException(AddressOf app_ThreadException),
                New Object() {sender, e})
                Else
                    ShowDebugOutput(e.Exception)
                End If
    
            End Sub
    
            Private Sub AppDomain_UnhandledException(ByVal sender As Object,
        ByVal e As UnhandledExceptionEventArgs)
    
                ShowDebugOutput(DirectCast(e.ExceptionObject, Exception))
    
            End Sub
    
            Private Sub MyApplication_UnhandledException(sender As Object,
                                                 e As ApplicationServices.UnhandledExceptionEventArgs) Handles Me.UnhandledException
              
                ShowDebugOutput(e.Exception)
    
            End Sub
    ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς μάλα πολλὰ
    πλάγχθη, ἐπεὶ Τροίης ἱερὸν πτολίεθρον ἔπερσεν·

  8. #8

    Thread Starter
    King of sapila
    Join Date
    Oct 2006
    Location
    Greece
    Posts
    6,605

    Re: [RESOLVED] Close a child form of MDI Form on Load

    Just to add that there was another problem that async load was crashing.
    When I used just exit sub inside an async statement, the whole app would crash.
    The issue was that exiting a loading form when async tasks took place, just did not go well with it.
    I've googled but did not find anything, thankfully I've struggled a lot with async cancellations last month so my idea was to use a Task.WaitAll() just before the exit sub.
    Again exit SUB not CLOSE() . That did the trick. And that is only necessary on Loading the form. On any other function included an identical function that has almost all the form_load code it does not need to use Task.WaitAll().
    Possible it would have worked with a cancellation token also but I did not try it.
    So, I would have to say, go figure and good riddance.
    ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς μάλα πολλὰ
    πλάγχθη, ἐπεὶ Τροίης ἱερὸν πτολίεθρον ἔπερσεν·

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width