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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With