Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. .NET (Core and Framework)
  4. Cleaning up resouces in a WPF application

Cleaning up resouces in a WPF application

Scheduled Pinned Locked Moved .NET (Core and Framework)
csharpwpfwinformshelpquestion
5 Posts 2 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    Simon P Stevens
    wrote on last edited by
    #1

    Hi, I've got a few classes that implement IDisposable to clean up their resources. In a windows forms app, I would create the objects in the forms constructor and dispose of the objects in the overridden dispose method. In WPF, the Window class doesn't implement IDisposeable. Where should I dispose of these objects? I'm quite new to WPF, so maybe I'm just missing something obvious. Can anyone help.

    Simon

    T 1 Reply Last reply
    0
    • S Simon P Stevens

      Hi, I've got a few classes that implement IDisposable to clean up their resources. In a windows forms app, I would create the objects in the forms constructor and dispose of the objects in the overridden dispose method. In WPF, the Window class doesn't implement IDisposeable. Where should I dispose of these objects? I'm quite new to WPF, so maybe I'm just missing something obvious. Can anyone help.

      Simon

      T Offline
      T Offline
      TJoe
      wrote on last edited by
      #2

      The WPF Window class doesn't implement IDisposable, because it doesn't have anything to dispose. You can create your own Window class (say MyWindow) that derives from Window and implements IDisposable. Then either you can explicitly call Dispose when you are done with your Window, or you can simply let the finalizer call it for you. Check this[^] and this[^] out, for more info on how to implement IDisposable properly.

      Take care, Tom ----------------------------------------------- Check out my blog at http://tjoe.wordpress.com

      S 1 Reply Last reply
      0
      • T TJoe

        The WPF Window class doesn't implement IDisposable, because it doesn't have anything to dispose. You can create your own Window class (say MyWindow) that derives from Window and implements IDisposable. Then either you can explicitly call Dispose when you are done with your Window, or you can simply let the finalizer call it for you. Check this[^] and this[^] out, for more info on how to implement IDisposable properly.

        Take care, Tom ----------------------------------------------- Check out my blog at http://tjoe.wordpress.com

        S Offline
        S Offline
        Simon P Stevens
        wrote on last edited by
        #3

        The problem won't be solved by implementing the dispose pattern on the window, I will still be left with a window class that doesn't get disposed as there is nowwhere to call dispose on the Window from. In WPF the entry point is auto generated and looks like this: /// /// Application Entry Point. /// [System.STAThreadAttribute()] [System.Diagnostics.DebuggerNonUserCodeAttribute()] public static void Main() { WpfApplication1.App app = new WpfApplication1.App(); app.InitializeComponent(); app.Run(); } (No window class anywhere), and the app.xml file looks like this <Application x:Class="WpfApplication1.App" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" StartupUri="Window1.xaml"> <Application.Resources> </Application.Resources> </Application> I don't have control over the creation of the window class, and it won't get disposed of. I tried it, and all that happens is the finaliser gets triggered when the app is closed. What I've done for now is to manually implement the app entry point so I have control over the window creation: public static void Main() { WpfApplication1.App app = new WpfApplication1.App(); using (Window1 mainWindow = new Window1()) { app.Run(mainWindow ); } } But this doesn't quite feel like the 'WPF way'. I have no app.xaml file. Is there a way of doing this without manually implementing the the app entry point, or is this how it should be done? It's a minor point really, It works fine the way I've done it, just I wondered if there's a proper way of doing it.

        Simon

        T 1 Reply Last reply
        0
        • S Simon P Stevens

          The problem won't be solved by implementing the dispose pattern on the window, I will still be left with a window class that doesn't get disposed as there is nowwhere to call dispose on the Window from. In WPF the entry point is auto generated and looks like this: /// /// Application Entry Point. /// [System.STAThreadAttribute()] [System.Diagnostics.DebuggerNonUserCodeAttribute()] public static void Main() { WpfApplication1.App app = new WpfApplication1.App(); app.InitializeComponent(); app.Run(); } (No window class anywhere), and the app.xml file looks like this <Application x:Class="WpfApplication1.App" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" StartupUri="Window1.xaml"> <Application.Resources> </Application.Resources> </Application> I don't have control over the creation of the window class, and it won't get disposed of. I tried it, and all that happens is the finaliser gets triggered when the app is closed. What I've done for now is to manually implement the app entry point so I have control over the window creation: public static void Main() { WpfApplication1.App app = new WpfApplication1.App(); using (Window1 mainWindow = new Window1()) { app.Run(mainWindow ); } } But this doesn't quite feel like the 'WPF way'. I have no app.xaml file. Is there a way of doing this without manually implementing the the app entry point, or is this how it should be done? It's a minor point really, It works fine the way I've done it, just I wondered if there's a proper way of doing it.

          Simon

          T Offline
          T Offline
          TJoe
          wrote on last edited by
          #4

          Following the proper implementation of IDisposable, the finalizer should call the Dispose method for you. So if your application is shutting down, and the finalizer is called on the Window, then Dispose should be called. Are you saying that Dispose is not being called in this scenario?

          Take care, Tom ----------------------------------------------- Check out my blog at http://tjoe.wordpress.com

          S 1 Reply Last reply
          0
          • T TJoe

            Following the proper implementation of IDisposable, the finalizer should call the Dispose method for you. So if your application is shutting down, and the finalizer is called on the Window, then Dispose should be called. Are you saying that Dispose is not being called in this scenario?

            Take care, Tom ----------------------------------------------- Check out my blog at http://tjoe.wordpress.com

            S Offline
            S Offline
            Simon P Stevens
            wrote on last edited by
            #5

            Sorry, thats not what I meant, I'm not very good at explaining. Yes the finalizer is getting called, and yes that triggers the dispose method, but following the pattern, it calls the dispose method and passes false, which means it doesn't clean up managed resources, instead they are left for their own finalizers to be triggered to clean them up, and so on, which means my whole stack of objects are all left waiting for their finalisers to clean them up which is a bit messy, which is what I meant when I said "it won't get disposed of...all that happens is the finaliser gets triggered." Also, finalizers aren't guranteed to be called. I think the way I've done it is the best solution, I get deterministic clean up, and no worries about relying on the finaliser. Thanks for helping.

            Simon

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups