Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to be sure an unmanaged resource to be cleaned

In my WinCE I have a WinForm:

public class SyncForm : Form
{
 IDisposable someResource;
 public SyncForm()
 {
  someResource = new SomeResource();
 }

        private void SyncForm_Closed(object sender, EventArgs e)
        {
            someResource.Dispose();
        }
}

It works fine when application is closing correctly, but the resource is not cleaned if an unhandled exception happens. Where should I put to be sure it is called even if my application crashes?

like image 524
Solvek Avatar asked Jan 20 '26 01:01

Solvek


1 Answers

Go to the designer file. Near the top will be an overridden Dispose method. Dispose of that resource in that Dispose method. This is the only method in the designer file that is designed to be edited by you, so Visual Studio won't overwrite content you write there. That said, if you're concerned, you can write a separate method in the normal code behind that you simply call from the Dispose method.

like image 83
Servy Avatar answered Jan 22 '26 15:01

Servy



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!