Results 1 to 6 of 6

Thread: [RESOLVED] Closing The Main Form Faster

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jun 2010
    Posts
    144

    Resolved [RESOLVED] Closing The Main Form Faster

    When I close my program, it takes like 5 seconds to completely close. Is this okay? I wonder if there is a faster way to close all the threads and any resources that might be loaded. Is the me.Close() enough? What about System.Windows.Forms.Application.Exit()? What do you usually do in the MainForm_Closed sub?

    Code:
        Private Sub MainForm_Closed(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles MyBase.Closed
            '
            ' If still connected to devices, disconnect & free all device data
            DestroyHidObjects()
            StopEngine()
            EngineOutput.Close()
    
            SerialPort2.DiscardInBuffer()   'CLEAR RECEIVED DATA BUFFER
            SerialPort2.Dispose()              'PROGRAM CRASHES SOMETIMES IF THIS LINE IS NOT INCLUDED
            SerialPort2.Close()
    
            'Environment.Exit(0)
            System.Windows.Forms.Application.Exit()
            Me.Close()
    
        End Sub

  2. #2
    Frenzied Member
    Join Date
    Nov 2017
    Posts
    1,981

    Re: Closing The Main Form Faster

    What code is in DestroyHidObjects and StopEngine? If you comment out those two method calls does the form close immediately?

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Jun 2010
    Posts
    144

    Re: Closing The Main Form Faster

    Quote Originally Posted by OptionBase1 View Post
    What code is in DestroyHidObjects and StopEngine? If you comment out those two method calls does the form close immediately?
    Good catch, but after commenting those out the program still takes time to close. What is the best way to close all the threads and resources in the Closed sub?

  4. #4
    PowerPoster Arnoutdv's Avatar
    Join Date
    Oct 2013
    Posts
    5,168

    Re: Closing The Main Form Faster

    Why is it a problem that your program needs time to clean up the mess?

  5. #5
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    37,412

    Re: Closing The Main Form Faster

    It's certainly doing something. It is possible that all it is doing is waiting for something unimportant to stop doing something you don't care about, but otherwise, you really WANT it to take the time to finish. So, you need to figure out what it is doing during that time, because until you know, you can't be sure that what it is doing isn't important.

    One thing you can do to figure this out is to create a Stopwatch object at form scope, then time different blocks of code. For example, you could start the stopwatch at the beginning of the method you showed, then stop it at the end of the method, and look at the EllapsedMilliseconds to see how long that took. If it is your five seconds, then you can move the stop earlier and earlier to figure out which rows are causing the time. You've already eliminated the first two lines, now you need to eliminate others.
    My usual boring signature: Nothing

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Jun 2010
    Posts
    144

    Re: Closing The Main Form Faster

    After running the program in debugger mode, I found out that during the MainForm_Closed method the program tried to discard incoming data buffers from SerialPort2, but that port was closed. Just by checking if the port was open before discarding the buffers fixed the problem. Now, the program closes immediately.

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