views:

760

answers:

5

I am getting this warning from FxCop:

"'RestartForm' contains field 'RestartForm.done' that is of IDisposable type: 'ManualResetEvent'. Change the Dispose method on 'RestartForm' to call Dispose or Close on this field."

Ok, I understand what this means and why this is what needs to be done... Except System.Windows.Forms.Form doesn't allow you to override either .Close() or .Dispose(), so what to do? Currently I'm running with this solution:

    private void RestartForm_FormClosing(object sender, FormClosingEventArgs e)
    {
        done.Set();
        done.Close();
    }

Which works as intended for my application... But FxCop still shows this message. Am I covered and can I safely ignore it, or is there another way I should be doing this?

A: 

Where done is the IDisposable field you mentioned.

using System;
using System.Windows.Forms;

public class FormDispose : Form, IDisposable
{
    void IDisposable.Dispose()
    {
        done.Dispose();
        base.Dispose();
    }
}
Matthew Flaschen
The base.Dispose() call should always be the last thing in a dispose method.
womp
Thanks, womp. Fixed.
Matthew Flaschen
A: 

You need to override the Dispose method, this method comes from the Control base class

protected override void Dispose(bool disposing)
{
  if (disposing)
  {
    event.Dispose();
  }
  base.Dispose(disposing);
}
Shay Erlichmen
+2  A: 

If RestartForm extends System.Windows.Forms.Form, you should be able to override Dispose(bool disposing). You should properly implement this for your "RestartForm" class to dispose of your IDisposables.

It should look like:

public override Dispose(bool disposing)
{
   if (disposing)
   {
       // Dispose was called from user code. Dispose of managed resources here.
       done.Dispose();
   }

   // Dispose of unmanaged resources here, and invoke base dispose.
   base.Dispose(disposing);
}
womp
+6  A: 

You need to override the Dispose method from Form

Typically this is automatically overridden in the RestartForm.Designer.cs file, so you will need to move the dispose into your code file so that you can add whatever code you need to add without it being rewritten by the designer.

In the RestartForm.cs

protected override void Dispose(bool disposing)
{
  if (disposing)
  {
    if (components != null))
    {
      components.Dispose();
    }

    // Dispose stuff here
  }

  base.Dispose(disposing);
}
heavyd
This is exactly what I was missing... I didn't even think to check if the designer was putting it in it's autogenerated code... Explains why it wasn't on the override list, it was already implemented, just not by me!
Matthew Scharley
+4  A: 

Take a look at my How To Dispose Members From Forms blog post - it should provide you with all the options in excrutiating details (particularly if you read the comments as well.)

Mark Seemann
-1 for linking to your blog. If you have a valuable contribution to make, make it here on StackOverflow. Plus your solution is rather inelegant.
Eric
I fail to see why Stack Overflow should be considered a closed system. If someone asks a question and the answer is already available on the internet, why not link to it? Isn't it about helping other people? Whether it's my own blog or not is not particularly relevant - I also link to other peoples' blogs, but I know my own one better, so I tend to use that when it addresses a particularly problem. On another note, can you explain why you find the solution inelegant?
Mark Seemann
There is nothing wrong with linking out - but perhaps *summarise* it here - I know (from lots of cleanup) that such links have a habit of disappearing; it would be good to have at least the high level ("executive summary") here - i.e. enough to point the next reader in the right vague direction, albeit without the full turn-by-turn map (if you see what I mean).
Marc Gravell
SO is designed to be an information repository, not some link farm pointing to external resources.
Eric
Knowing that a particular piece of information exists on the internet and sharing that knowledge is part of being an information repository as well. Would you have downvoted if I had linked to Wikipedia as well? If you have a problem with that, you can downvote half the answers on SO.
Mark Seemann
I have Google for providing me with links to existing information on the internet. I have SO for providing me with answers to questions. Wikipedia doesn't have articles where the entire body of the article says "This has already been described by Encyclopedia Britannica, which you can find in your local library". That's the intellectual equivalent of your answer here.
Eric
I disagree, but let's examine what's going on here: A user asks this particular question here on SO. Perhaps he already used a search engine and didn't find my blog post, or perhaps he never searched. I came across the question and knew that I had much earlier written a blog post that provides an answer, so I pointed the OP to that post. How does that not add value to the OP?
Mark Seemann
Take a look at the broader perspective: If you check the dates, that blog post predates my answer by almost two years. I originally wrote the post because I believed it provided value in itself, irrespective of SO (you may disagree, but that's a different discussion). No-one paid me to do it. I did it to share knowledge, and you think I should be *punished* for doing that?!
Mark Seemann
You seem to be only considering the short term. While your answer adds some marginal value to the discussion in the here and now, what value does your answer add if the link to your blog post stops working or the content of your blog post is deleted? By refusing to include any meaningful content in your answer here on SO, you and other SO posters like you, are forcing the SO community to rely on you to continue to provide the answer content. That's contrary to the spirit of SO, which is intended to be a source of answers to questions, not a source of broken links and frustration.
Eric
In addition, your hostile attitude about this suggests that the most important things to you are getting more traffic to your blog and getting more reputation by providing a quick answer with minimal effort on your part.
Eric
And finally, yes, I do think you should be 'punished'. The whole idea of the point system on SO is to reward users who take the time to provide useful answers to questions, not just for the person asking the question, but for anyone in the future who might have the same question. If you can't be bothered to do more in providing your answer than copy and paste a link, especially a self-promoting link, then don't expect a pat on the back for it.
Eric
Your argument is based on wrong assumptions: I don't own the blog in question: Microsoft does. That URL is unlikely to disappear as long as Microsoft is in business. There are no ads on the site, and I don't work for MS any longer, so I get absolutely nothing out of driving traffic to that site.
Mark Seemann
In any case, you are welcome to state your opinions on how you think SO should work, but I have a different opinion. Neither of us are entitled to dictate how SO should work.
Mark Seemann