473,785 Members | 2,188 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Executing code at function exit

Hi,

I would like to be able to execute code when a function exits. In c++ I
would have created an object on the stack in the function entry point
and it's destructor would be called when the function exits as
following:
void func()
{
MyObj obj;
.....
} <- Destructor code of MyObj called here.

I could be more explicit by writing this code in C#:
void func()
{
.....DoSomethin g...
ExitFunction();
}

But note that as opposed to the C++ code, ExitFunction() is not called
in the case an exception was thrown from func(). I could use the same
code in C#, but since the GC is not determinstic my ExitFunction() code
wouldn't be called in time.

You may ask why I want code to run at the function exit if the
instruction flow hasn't reached the end of the code, but this could be
very useful for building a logging infrastructure where I could show
the flow of function calls in my application.

Any suggestions how this can be solved?

Thanks,

Danny

Jul 23 '06 #1
4 4684
Place a try - finally statement within your function.
In that sense, you can be sure that even in case of throw the finally is
executed:
void func()
{
try {
....DoSomething ...
}
finally{
ExitFunction();
}

}

- José
"Danny Liberty" <dl******@gmail .coma écrit dans le message de news:
11************* *********@s13g2 00...legr oups.com...
Hi,

I would like to be able to execute code when a function exits. In c++ I
would have created an object on the stack in the function entry point
and it's destructor would be called when the function exits as
following:
void func()
{
MyObj obj;
....
} <- Destructor code of MyObj called here.

I could be more explicit by writing this code in C#:
void func()
{
....DoSomething ...
ExitFunction();
}

But note that as opposed to the C++ code, ExitFunction() is not called
in the case an exception was thrown from func(). I could use the same
code in C#, but since the GC is not determinstic my ExitFunction() code
wouldn't be called in time.

You may ask why I want code to run at the function exit if the
instruction flow hasn't reached the end of the code, but this could be
very useful for building a logging infrastructure where I could show
the flow of function calls in my application.

Any suggestions how this can be solved?

Thanks,

Danny

Jul 23 '06 #2
Of course, but since I'm writing a logging framework I would like to
minimize the amount of code required to do this. If I have to put a try
/ catch statement for the entire scope of every function it would make
my code extremely "dirty"...
José Joye wrote:
Place a try - finally statement within your function.
In that sense, you can be sure that even in case of throw the finally is
executed:
void func()
{
try {
....DoSomething ...
}
finally{
ExitFunction();
}

}

- José
"Danny Liberty" <dl******@gmail .coma écrit dans le message de news:
11************* *********@s13g2 00...legr oups.com...
Hi,

I would like to be able to execute code when a function exits. In c++ I
would have created an object on the stack in the function entry point
and it's destructor would be called when the function exits as
following:
void func()
{
MyObj obj;
....
} <- Destructor code of MyObj called here.

I could be more explicit by writing this code in C#:
void func()
{
....DoSomething ...
ExitFunction();
}

But note that as opposed to the C++ code, ExitFunction() is not called
in the case an exception was thrown from func(). I could use the same
code in C#, but since the GC is not determinstic my ExitFunction() code
wouldn't be called in time.

You may ask why I want code to run at the function exit if the
instruction flow hasn't reached the end of the code, but this could be
very useful for building a logging infrastructure where I could show
the flow of function calls in my application.

Any suggestions how this can be solved?

Thanks,

Danny
Jul 23 '06 #3
I would recommend looking at AOP (if not aop atleast the concept of using a
dynamic proxy to apply these items).

Luckily you picked a logging example, there are about 1000000 examples of
AOP doing logging :)

Cheers,

Greg
"Danny Liberty" <dl******@gmail .comwrote in message
news:11******** **************@ p79g2000cwp.goo glegroups.com.. .
Of course, but since I'm writing a logging framework I would like to
minimize the amount of code required to do this. If I have to put a try
/ catch statement for the entire scope of every function it would make
my code extremely "dirty"...
José Joye wrote:
Place a try - finally statement within your function.
In that sense, you can be sure that even in case of throw the finally is
executed:
void func()
{
try {
....DoSomething ...
}
finally{
ExitFunction();
}

}

- José
"Danny Liberty" <dl******@gmail .coma écrit dans le message de news:
11************* *********@s13g2 00...legr oups.com...
Hi,

I would like to be able to execute code when a function exits. In c++ I
would have created an object on the stack in the function entry point
and it's destructor would be called when the function exits as
following:
void func()
{
MyObj obj;
....
} <- Destructor code of MyObj called here.

I could be more explicit by writing this code in C#:
void func()
{
....DoSomething ...
ExitFunction();
}

But note that as opposed to the C++ code, ExitFunction() is not called
in the case an exception was thrown from func(). I could use the same
code in C#, but since the GC is not determinstic my ExitFunction() code
wouldn't be called in time.

You may ask why I want code to run at the function exit if the
instruction flow hasn't reached the end of the code, but this could be
very useful for building a logging infrastructure where I could show
the flow of function calls in my application.

Any suggestions how this can be solved?

Thanks,

Danny

Jul 23 '06 #4
Actually I did look into AOP. Most implementations use ContextBound and
IMessageSink to intercept the call to the function. Generally this
works, but if the function throws an exception it cannot be caught by
the original caller since the "real" caller of the function is actually
the transparent proxy created by using the message sink. In other
words, the function is being called from SyncProcessMess age() and since
the exception is not caught there it is not propogated to the original
caller. A workaround could be to put the code in SyncProcessMess age()
in a try-catch block, but this would have a great impact on performance
(not to mention the price I've already paid for inheriting from
ContextBound).

Actually, it looks like .Net lacks support for an efficient AOP
implementation.
Current implementations for .net seem to be awkward. The use of objects
and interfaces originaly designed for remoting to achieve this seems to
me like a huge and ugly workaround.

Anyway, suggested solutions will be greatly appreciated :)

Danny
Greg Young wrote:
I would recommend looking at AOP (if not aop atleast the concept of usinga
dynamic proxy to apply these items).

Luckily you picked a logging example, there are about 1000000 examples of
AOP doing logging :)

Cheers,

Greg
"Danny Liberty" <dl******@gmail .comwrote in message
news:11******** **************@ p79g2000cwp.goo glegroups.com.. .
Of course, but since I'm writing a logging framework I would like to
minimize the amount of code required to do this. If I have to put a try
/ catch statement for the entire scope of every function it would make
my code extremely "dirty"...
José Joye wrote:
Place a try - finally statement within your function.
In that sense, you can be sure that even in case of throw the finally is
executed:
void func()
{
try {
....DoSomething ...
}
finally{
ExitFunction();
}

}

- José
"Danny Liberty" <dl******@gmail .coma écrit dans le message de news:
11************* *********@s13g2 00...legr oups.com...
Hi,
>
I would like to be able to execute code when a function exits. In c++I
would have created an object on the stack in the function entry point
and it's destructor would be called when the function exits as
following:
void func()
{
MyObj obj;
....
} <- Destructor code of MyObj called here.
>
I could be more explicit by writing this code in C#:
void func()
{
....DoSomething ...
ExitFunction();
}
>
But note that as opposed to the C++ code, ExitFunction() is not called
in the case an exception was thrown from func(). I could use the same
code in C#, but since the GC is not determinstic my ExitFunction() code
wouldn't be called in time.
>
You may ask why I want code to run at the function exit if the
instruction flow hasn't reached the end of the code, but this could be
very useful for building a logging infrastructure where I could show
the flow of function calls in my application.
>
Any suggestions how this can be solved?
>
Thanks,
>
Danny
>
Jul 25 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
4010
by: Daveyk0 | last post by:
Hello there, I have a front end database that I have recently made very many changes to to allow off-line use. I keep copies of the databases on my hard drive and link to them rather than the live databases on the network. Is there a way, via code, when I get back in-house from being on the road to click a button, and select the backends I want to link to? I would want to delete all the current links and link to the "live"
0
1816
by: Rahul Chatterjee | last post by:
Hello All I have designed a dotnet application using VB which basically takes a selection and passes value to a crystal report which in turn passes the value to a Stored procedure. After the report gets generated it is PDF'ed - all this is done thru a single click in my program. Now the catch here is that through the program all these tasks of generating the report and PDF'ing 1400 records (about 25 pages) takes 15 minutes.
7
13586
by: fniles | last post by:
I am using VB.Net 2003 and MS Access (connecting using OleDBConnection). I read using DataAdapter and DataSet, not DataReader. When many people try to access the database at the same time, I get the error "ExecuteReader requires an open and available Connection. The connection's current state is Open, Executing." I do not use ExecuteReader, why the error says ExecuteReader. What does it mean ? When I get this error, is there a way for me...
7
2841
by: wingsss | last post by:
i am currently using WindowsXP SP2 all updated apache 2.2.4 PHP 5.2.3 MySQL 5.0 and apache hang up when running this code // ------------------------------------------------------------------------------ function query($query="show tables")
3
3897
by: DontB3 | last post by:
Hi, I'm new in this forum, and i hope someone can help. I'm creating an automatic application that transfer a database from Access -> DBF -> Oracle. When My App try to execute Insert SQL statement from DBF to Oracle database i found an error like this {Run-time error "7": Error executing statement! Error: System resource exceeded.} and then my program crash! i have to close my app, and start again. could some one help me to...
36
3117
by: The Frog | last post by:
Hi Everyone, I am trying to find a solution for handling zipped data without the need to ship / install any DLL files with the database. Does anybody know of code to handle ZIP files that does not require any external references? If I can ship it 'built-in' as either a class module or standard module then that would be perfect. Any help would be greatly appreciated.
2
2576
by: Swan | last post by:
How can I restrict alt+tab and start menu from keyboard while program executing(VB)?I am posting what I tried-- form.frm Option Explicit Private Sub Form_Load() HookKeyboard End Sub Private Sub Form_Unload(Cancel As Integer) UnhookKeyboard
0
1565
Frinavale
by: Frinavale | last post by:
I have a peculiar problem... Background: I have a function that I don't want the user to execute more than once while they are waiting for it to process; therefore, I disable all of the controls on the page via some JavaScript before the request is sent. This function takes some time to execute because it has to communicate with hardware that is rather slow. This slowness combined with a little bit of lag sometimes results in a ...
1
2949
by: prdola | last post by:
Hi, I have a table with function names and some other flags where to run them. Sth like this table called PROCESS name run_daily run_monthly func1 Y N func2 Y Y func3 N Y ... then I have function that should execute selected functions sth like
0
9484
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10350
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10157
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8983
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7505
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6742
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5386
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4055
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2887
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.