Repost: Is there a way to be notified when a method terminates

J

Joshua Frank

Hi All,

I posted this a while back without much success, so I thought I'd again.
Basically, I want to set things up so that I can be notified when a
method terminates:

Sub Test()
'indicate that we want to be notified when Test terminates
RegisterForEvent(AddressOf MyCallbackFunction)

...
End Sub

Then when Test terminates, the framework would call the callback
function. I've looked into the profiling API, but this is more than I
need, and would be very difficult to implement for various reasons. And
yes, I know that this is more or less the same as:

Sub Test()
Try
...
Finally
MyCallbackFunction()
End Try
End Sub

but I want to avoid the Try block, preferring to save this construct for
situations where exceptions may be thrown.

So does anyone know a way to do this? I think it ought to be possible
to work with the framework, or maybe hack a little to replace the return
location on the stack with MyCallbackFunction, rather than the true
caller. But I'd really like to figure this one out. TIA.

Cheers,
Josh
 
S

sancerre

Josh,

Here is an example of using a callback delegate - the code is in C#...

Step One: declare a type of delegate:
public delegate void TellMeNow();

Step Two: create a method with the same signature as the delegate (in
this case, a method with no parameters):
private void doSomethingWhenTold()
{
// this is the method that will get invoked!
}

Step Three: instantiate the callback with a reference to the method
created in step two
callback = new TellMeNow(doSomethingWhenTold);

Step Four: create a method where you want the notification to be
triggered. Use the delegate type for one of the parameters.
public AMethod(TellMeNow callBack)
{
// Do some work, then trigger the call back method
callBack();
}

-- Sancerre
 
G

Gabriele G. Ponti

Rewrite MSIL Code on the Fly with the .NET Framework Profiling API
http://msdn.microsoft.com/msdnmag/issues/03/09/NETProfilingAPI/default.aspx

From the article summary: "In this article, the author shows how to
dynamically rewrite Microsoft Intermediate Language code on the fly using
the Profiling API of the CLR. Unlike approaches based on Reflection.Emit,
this scheme works with the existing assemblies and doesn't require the
creation of proxy or dynamic assemblies. The needs for IL code rewriting
emerges when you want to make your changes transparent to the client and
preserve the identity of classes. This technique can be used for creation of
interceptors, pre- and post-processing method calls, and code
instrumentation and verification."
 
J

Joshua Frank

I understand how callbacks work in general, but in this case I want the
dotnet framework to call me back, not my own code. What I was asking is
if there's any way to have the framework call back when a method is
terminating, and since that's not my code, your suggestion doesn't get
me all the way there.
 
J

Joshua Frank

I was just reading this article this morning, in my quest to solve this
problem. It's the closest I've come to solving this problem, so thanks
for finding it for me! Unfortunately:

1. "Insertion of an epilogue requires a lot of work and, in the very
generic case, is extremely complicated. First, you have to identify all
the points where the method may return." The article goes on to list a
number of ways that you will do this incorrectly and cause hideous bugs.

2. "This approach...[is] not suited for deployment in a production
environment." Because the profiling API is really for use on a
developer's machine to access the internals for profiling purposes, not
to base code on. Therefore various things will either not work in
production, or else cause other things to not work. And I think it
would cause a performance hit as well.

So I think I'm still in need of a good solution for this. Thanks so far
though.

Cheers,
Josh
 
R

Robert Jordan

Hi Joshua,

[...]
Then when Test terminates, the framework would call the callback
function. I've looked into the profiling API, but this is more than I
need, and would be very difficult to implement for various reasons. And
yes, I know that this is more or less the same as:

Sub Test()
Try
...
Finally
MyCallbackFunction()
End Try
End Sub

but I want to avoid the Try block, preferring to save this construct for
situations where exceptions may be thrown.

You can nest try-finally blocks, at least in C#:

try {
try {
}
finally {
// for situations where exceptions may be thrown
}
}
finally {
MyCallbackFunction();
}

So does anyone know a way to do this? I think it ought to be possible
to work with the framework, or maybe hack a little to replace the return
location on the stack with MyCallbackFunction, rather than the true
caller. But I'd really like to figure this one out. TIA.

Just another cool hack:

http://www.codeguru.com/Csharp/.NET/net_general/patterns/article.php/c7861/

bye
Rob
 
S

sancerre

Now I understand your question...

Wouldn't spawning off a separate thread achieve your desired result?
-- Sancerre
 
J

Joshua Frank

Thanks for the lead. I came across the ContextBoundObject approach
while trying to solve this problem, and at first it seems like a decent
way to go, but there are some fatal problems.

1. It's too complicated.
2. There's significant overhead that makes it too expensive to actually
use.
3. Once you inherit from ContextBoundObject, you can't inherit from
anything else, which really messes up your design options.

So I think I have to keep looking.

Cheers,
Josh

Robert said:
Hi Joshua,

[...]
Then when Test terminates, the framework would call the callback
function. I've looked into the profiling API, but this is more than I
need, and would be very difficult to implement for various reasons.
And yes, I know that this is more or less the same as:

Sub Test()
Try
...
Finally
MyCallbackFunction()
End Try
End Sub

but I want to avoid the Try block, preferring to save this construct
for situations where exceptions may be thrown.


You can nest try-finally blocks, at least in C#:

try {
try {
}
finally {
// for situations where exceptions may be thrown
}
}
finally {
MyCallbackFunction();
}

So does anyone know a way to do this? I think it ought to be possible
to work with the framework, or maybe hack a little to replace the
return location on the stack with MyCallbackFunction, rather than the
true caller. But I'd really like to figure this one out. TIA.


Just another cool hack:

http://www.codeguru.com/Csharp/.NET/net_general/patterns/article.php/c7861/

bye
Rob
 
G

Guest

Joshua Frank said:
Hi All,

I posted this a while back without much success, so I thought I'd again.
Basically, I want to set things up so that I can be notified when a
method terminates:

Sub Test()
'indicate that we want to be notified when Test terminates
RegisterForEvent(AddressOf MyCallbackFunction)

...
End Sub

Then when Test terminates, the framework would call the callback
function. I've looked into the profiling API, but this is more than I
need, and would be very difficult to implement for various reasons. And
yes, I know that this is more or less the same as:

Sub Test()
Try
...
Finally
MyCallbackFunction()
End Try
End Sub

but I want to avoid the Try block, preferring to save this construct for
situations where exceptions may be thrown.

So does anyone know a way to do this? I think it ought to be possible
to work with the framework, or maybe hack a little to replace the return
location on the stack with MyCallbackFunction, rather than the true
caller. But I'd really like to figure this one out. TIA.

Cheers,
Josh
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top