This article is a mirror article of machine translation, please click here to jump to the original article.

View: 20162|Reply: 0

[Source] {"Cannot access a disposed object.\r\nObject name: 'Form1'."}.

[Copy link]
Posted on 6/12/2016 4:28:41 PM | | | |

How to avoid exceptions when closing a form when calling a control across threads cannot access disposed objects.
When calling a form across threads in a thread, and when using invoke to assign values or change properties to form controls, if the thread is not terminated before the form is closed, an exception like this will always pop up: cannot access a disposed object.
The reason should be that when the form control is called in the thread, the form has been logged out, so it exits all threads before the form is logged out. To solve this problem, you can add the following code:


will no longer pop up the exception.
Calling system.environment.exit(0 can completely exit the program, no matter what thread is forced to quit, ending the program very cleanly.



FormClosing event

When the form is closed, the FormClosing event occurs. This event is handled. This frees up all resources related to the form.

If you cancel this event, the form remains open.

When the form appears as a mode dialog, clicking Close hides the form and sets the DialogResult property to Cancel. By setting the DialogResult property in some events, the value of the DialogResult can be overridden when the user clicks the close button in the upper right corner.

FormClosed event

A FormClosed event occurs after the Close or Exit method of the user or Application class closes the form.

You can use this event to release some of the form's resources. You can also use this event to save some information in the input form or to update the parent form.





Previous:C#/Winform version 12306 login, you can manually click the image verification code
Next:C# socket to get the client IP address and port
Disclaimer:
All software, programming materials or articles published by Code Farmer Network are only for learning and research purposes; The above content shall not be used for commercial or illegal purposes, otherwise, users shall bear all consequences. The information on this site comes from the Internet, and copyright disputes have nothing to do with this site. You must completely delete the above content from your computer within 24 hours of downloading. If you like the program, please support genuine software, purchase registration, and get better genuine services. If there is any infringement, please contact us by email.

Mail To:help@itsvse.com