-
May 21st, 2022, 05:23 PM
#1
Thread Starter
Addicted Member
[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
-
May 21st, 2022, 05:26 PM
#2
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?
-
May 21st, 2022, 05:32 PM
#3
Thread Starter
Addicted Member
Re: Closing The Main Form Faster
Originally Posted by OptionBase1
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?
-
May 22nd, 2022, 02:23 AM
#4
Re: Closing The Main Form Faster
Why is it a problem that your program needs time to clean up the mess?
-
May 22nd, 2022, 10:24 AM
#5
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
-
May 22nd, 2022, 03:15 PM
#6
Thread Starter
Addicted Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|