0

Is there any way to add an event to the current thread that is executed when the thread ends.

I have a solution where we create resources that we want to re-use but still need to have disposed when the thread ends to prevent leaks and I cannot trust every one to be diligent in adding manual dispose calls to their code.

I could do this with a wrapper around the thread but that will not be a reliable solution as I will not be able to make 100 % sure every one would use the wrapper and sometimes this might need to be added into an existing thread.

So I would need my resource to be able to add a dispose event to be called when the tread finishes.

Is this possible in C#

David Mårtensson
  • 7,550
  • 4
  • 31
  • 47
  • Look at this question http://stackoverflow.com/questions/5551258/c-net-how-to-alert-program-that-the-thread-is-finished-event-driven – Ilya Ivanov Jan 10 '13 at 09:13
  • 2
    *I cannot trust every one to be diligent in adding manual dispose calls to their code.* - Then why do you trust them to call dispose on any other object in .NET that implements `IDisposable`? – Rotem Jan 10 '13 at 09:15
  • @Rotem, I don't, I just try to improve on a situation I have, and are looking for options :/ – David Mårtensson Jan 10 '13 at 14:37

3 Answers3

0

Is there any way to add an event to the current thread that is executed when the thread ends?

No, there's no event in the framework that you can subscribe to.

As you say, one solution would be to wrap the thread main method with a try / finally block.


Another solution would be to rely on the GC. If someone forgets to call Dispose, the destructor will still be called some time after the object goes out of scope - it will be called on the finalizer thread, though.

Nick Butler
  • 24,045
  • 4
  • 49
  • 70
  • "destructor will still be called when the object goes out of scope" It gets called when the GC gets to it which may be some time after the object has gone out of scope. This is the whole reason why we have the Dispose pattern. – Martin Brown Jan 10 '13 at 09:53
  • @MartinBrown Absolutely - I've clarified my answer. – Nick Butler Jan 10 '13 at 09:59
  • Thats how it works today when someone uses the resources the wrong way. I was just fishing for any way to work around some developer errors :/ But I will have to settle for keeping a watch on resource usage and try to find any leaks. – David Mårtensson Jan 10 '13 at 14:36
0

If you expect developers may forget to call Dispose() on object, there is another pattern which can be used to force them to call such methods. This is inspired from how database transactions work.

The idea is you use the object which exposes two methods. Complete() and RollBack(). At the end of object lifespan, developer needs to call Complete() for things to work. If he fails to comply, object's destructor (or Dispose()) will call RollBack(). In .NET TransactionScope class is a good example.

In your case you need to encapsulate your thread object inside this class. The caller doesn't directly deal with thread object, but it deals with this class object.

Ankush
  • 2,454
  • 2
  • 21
  • 27
  • I will not be able to enforce using a wrapper around thread in this case :( – David Mårtensson Jan 10 '13 at 14:30
  • @DavidMårtensson How is caller invoking thread? the method executed by thread belongs to which set of classes? – Ankush Jan 10 '13 at 14:33
  • In this case it could be in any of a number of ways. This is a module used in many of our internal solutions to share data and I do not have control over the usages, unfortunately. – David Mårtensson Jan 10 '13 at 16:29
0

are we all talking about the same .net here? just impliment Finalize

http://msdn.microsoft.com/en-us/library/b1yfkh5e(v=vs.100).aspx

http://msdn.microsoft.com/en-us/library/66x5fx1b.aspx

this is the way to tell the 'GC' to do some additional actions when clearing a resource.

Nahum
  • 6,959
  • 12
  • 48
  • 69
  • The problem is that there might be situations where the object might take some time before it goes out of scope causing resources to be hanging for to long :(, The hope was to have something happen immediately when the thread ends. – David Mårtensson Jan 10 '13 at 14:30
  • I will name this one as answer since the finalize solution might improve on my solution, I was not aware of that Dispose was not called by GC, and this could be the cause of some of the problems I have seen, though it might take months before I can be sure :/ – David Mårtensson Jan 10 '13 at 16:33
  • Dispose is just a pretty name to tell the user the class might have unmanaged data to release. – Nahum Jan 10 '13 at 18:38