none
how dispose object used in secondary thread RRS feed

  • Question

  • hi, i have a winform application (.net 3.5) which have a "exit" button that will dispose it and call Application.Exit();

     while this application is running i have a background thread that inside an infinite loop will execute a job where it uses same disposable objects with a "using" statement.

    my problem is that when the application will exit the dispose/finalizer method of the disposable objects will never be called so i don't know how clean the resources used by that thread.

    how can i do it?

    thanks

    Monday, March 11, 2013 4:23 PM

Answers

  • The thread doesn't get killed, it just stops executing. When the process exits the OS cleans up the resources, including the thread.
    Monday, March 11, 2013 5:21 PM
  • Hi there,

    >>if there's a way to force the call of the finalizer method (or something similar) of an object when the application will exit.

    I agree with Jared: "When the process exits the OS cleans up the resources, including the thread." include the application resource.

    Or you can try this event in winforms: http://msdn.microsoft.com/en-us/library/system.windows.application.exit.aspx  

    You can  call your own method in this event handler.

    Best regards,


    Mike Feng
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Tuesday, March 12, 2013 12:51 PM
    Moderator
  • If you're doing this for the sake of cleaning up native resources, don't worry about it, the OS will do it. If, for some reason, the dispose method is making modifications beyond cleaning up native resources there is something you can do.

    Create a global variable named exiting and set it to true in the Exit method. Create a ManualResetEvent and have the Exit method wait on that event, after setting exiting to true.

    Then in the methods that you want Exit to wait on, at the beginning of the method use the Interlocked.Increment method and wrap the rest of the method in a try/finally. In the finally call Interlocked.Decrement, and if it returns 0 set the ManualResetEvent.

    Now what I described above technically has the possibility for a deadlock if the background operation gets called a lot. So to make sure that 0 is only reached as part of exiting, initialize the outstandingWork variable (or whatever you call that gets passed into Interlocked.Increment) to 1, and have Exit call Interlocked.Decrement. If Decrement returns 0, don't wait on the ManualResetEvent.

    Tuesday, March 12, 2013 10:39 PM

All replies

  • The OS cleans up the object when the process exits.

    Dispose is for when you need to clean up an object and the process is not exiting.

    Monday, March 11, 2013 4:26 PM
  • i hope that secondary thread were killed with an excpetion (as threadabort) so i tried with the using disposable pattern, anyway i tried also implementing finalizer but it is not called
    Monday, March 11, 2013 4:49 PM
  • The thread doesn't get killed, it just stops executing. When the process exits the OS cleans up the resources, including the thread.
    Monday, March 11, 2013 5:21 PM
  • sorry if my englishis not clear but i have an object that needs a specific action to clean the resources that it uses and i don't know how to execute it.

    my question is if there's a way to force the call of the finalizer method (or something similar) of an object when the application will exit.

    Tuesday, March 12, 2013 8:10 AM
  • Hi there,

    >>if there's a way to force the call of the finalizer method (or something similar) of an object when the application will exit.

    I agree with Jared: "When the process exits the OS cleans up the resources, including the thread." include the application resource.

    Or you can try this event in winforms: http://msdn.microsoft.com/en-us/library/system.windows.application.exit.aspx  

    You can  call your own method in this event handler.

    Best regards,


    Mike Feng
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Tuesday, March 12, 2013 12:51 PM
    Moderator
  • If you're doing this for the sake of cleaning up native resources, don't worry about it, the OS will do it. If, for some reason, the dispose method is making modifications beyond cleaning up native resources there is something you can do.

    Create a global variable named exiting and set it to true in the Exit method. Create a ManualResetEvent and have the Exit method wait on that event, after setting exiting to true.

    Then in the methods that you want Exit to wait on, at the beginning of the method use the Interlocked.Increment method and wrap the rest of the method in a try/finally. In the finally call Interlocked.Decrement, and if it returns 0 set the ManualResetEvent.

    Now what I described above technically has the possibility for a deadlock if the background operation gets called a lot. So to make sure that 0 is only reached as part of exiting, initialize the outstandingWork variable (or whatever you call that gets passed into Interlocked.Increment) to 1, and have Exit call Interlocked.Decrement. If Decrement returns 0, don't wait on the ManualResetEvent.

    Tuesday, March 12, 2013 10:39 PM