2

I have a basic form that will attempt to load and play a video of the user. The form can be called from a public static ShowDialogForm method. That method will attempt to initialize a video if successful then the form is returned otherwise the action is canceled. At my office we use a refactoring tool, and it is complaining about the lack of a using statement in my method. So my question is benifit, if any does the using statement offer in this instance.

This is the original code

public static DialogResult ShowDialogForm(VideoNames videoName, Course course, IWin32Window parent)
{
   FlashPlayer form = new FlashPlayer();

   if (form.Initialize(videoName, course))
   {
     return form.ShowDialog(parent);
   }
   else
   {
     return DialogResult.Cancel;
   }
}

This is the code suggested by the refactoring tool

public static DialogResult ShowDialogForm(VideoNames videoName, Course course, IWin32Window parent)
{
  using (FlashPlayer form = new FlashPlayer())
  {
    if (form.Initialize(videoName, course))
    {
      return form.ShowDialog(parent);
    }
    else
    {
      return DialogResult.Cancel;
    }
  }
}
4
  • 1
    if FlashPlayer inherits from System.Windows.Forms, then it implements IDisposible and using a using automatically calls Dispose() when you are done using the form. Commented Aug 31, 2011 at 14:46
  • possible duplicate of What is the C# Using block and why should I use it? Commented Aug 31, 2011 at 14:56
  • So the idea is that I do not need to put Dispose() calls in the events that cause the form to close, like click the x button, or the done link? Commented Aug 31, 2011 at 14:56
  • what tool are you using? Commented Aug 31, 2011 at 15:09

5 Answers 5

2

Yes, you should encapsulate any objects that implement IDisposable within a using block to ensure that it is properly disposed of by the GC. You should be especially cognizant of this within a winforms or wpf application where the memory and processes need to be more tightly controlled.

Sign up to request clarification or add additional context in comments.

Comments

1

Well, the tool's complaint is valid. When you display a form with ShowDialog() then the form object doesn't automatically get disposed like it does when you use Show(). This is important, you normally use a dialog to let the user enter values that you then retrieve after ShowDialog returns. Having the form controls disposed makes that risky, likely to trigger an ObjectDisposedException.

So you always wrap the dialog creation, display and result retrieval with a using statement so the form gets disposed after everything is done.

Note however that you don't actually use this dialog to retrieve anything. Which probably means it shouldn't be a dialog at all. So use Show() and you don't have to dispose it. And the user gets the freedom to continue using the rest of your user interface, assuming that's appropriate. Non-modal user interfaces are always preferred.

2 Comments

The idea was to prevent the user from interacting with the rest of the interface while the video is playing. Is there a more appropriate way of doing this?
Well, a dialog certainly does that. It is not the only way, you could for example set the Play button's Enable property to false so that the user can't start the video again. Set it back to true with the FormClosed event. It is up to you.
1

The using statement will wrap your use of the 'IDisposable' FlashPlayer in a try-finally block, which means the FlashPlayer will be disposed when you are finished. This is considered good practice as it releases the resource.

Normally if something implements IDisposable, it is good practice to wrap it in a using statement, or manually clean up after yourself by calling flashPlayer.Dispose().

Comments

1

The benefit may not be significant, but where IDisposable is implemented on a class, the using statement ensures that the code will execute safely. If the class implements it, there is most likely a reason for it.

Comments

1

Your tool is correct. Since forms are disposable, once you're done with it. You have to dispose. Using statements does this for you automatically.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.