473,322 Members | 1,425 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

overload lock command? maybe others?

I'd like to be able to overload the lock command so that I could log
an entry on lock and unlock. Any ideas on how to do this?

I think it would be powerful if you could inherit from a few keywords
in C# like this example:

static keyword MyLock : lock {
public MyLock(object obj) : base(obj) { log a message when entering
scope }
public ~MyLock() { log a message when going out of scope }
}

I could also see it being useful with the "using" keyword. I could see
it being very useful with try/catch/finally, but I think that would
conflict with the current method used for interrupting the code flow
(a painfully slow OS-level registration and interrupt).

Mar 15 '07 #1
8 1985
not_a_commie wrote:
I'd like to be able to overload the lock command so that I could log
an entry on lock and unlock. Any ideas on how to do this?
Create an object that implements IDisposable, and use

using (MyLock.Lock(foo))
{
}

.... instead.

E.g. roughly:

class MyLock : IDisposable
{
object _obj;

public MyLock(object obj)
{
_obj = obj;
if (_obj != null)
{
Monitor.Enter(_obj);
// logging
}
}

public void Dispose()
{
if (_obj != null)
{
Monitor.Exit(_obj);
// logging
}
_obj = null;
}
}
I could also see it being useful with the "using" keyword.
You can use the above idiom with 'using' too, by 'using' a proxy, and
passing the actual object to the constructor, and exposing it via a
property of the proxy.
I could see
it being very useful with try/catch/finally,
Another technique is to pass the code whose context you want to wrap as
an anonymous delegate. E.g.:

delegate void Proc();

static void MyWrapper(Proc proc)
{
try
{
proc();
}
catch (Exception ex)
{
// do something with ex, e.g. show a dialog
}
}

// ...

MyWrapper(delegate
{
// My code that might throw
});
but I think that would
conflict with the current method used for interrupting the code flow
(a painfully slow OS-level registration and interrupt).
-- Barry

--
http://barrkel.blogspot.com/
Mar 15 '07 #2
On Mar 15, 12:30 pm, Barry Kelly <barry.j.ke...@gmail.comwrote:
Create an object that implements IDisposable, and use

using (MyLock.Lock(foo))
{

}
The C# team could have eliminated a keyword had they used that pattern
to begin with. It also would be less confusing for novice developers
who try to figure out the purpose of a Monitor.Pulse or Monitor.Wait
embedded in a lock block, not realizing that the lock keyword and the
Monitor class are intimately related. Why did they choose to create
the lock keyword anyway?

Mar 15 '07 #3
Barry Kelly wrote:
using (MyLock.Lock(foo))
class MyLock : IDisposable
{
Missing, is of course:

public static MyLock Lock(object obj)
{
return new MyLock(obj);
}
object _obj;
I've used this technique to get nice syntax for other primitives, such
as Mutex, ReaderWriterLock, etc.

-- Barry

--
http://barrkel.blogspot.com/
Mar 15 '07 #4
Barry Kelly wrote:
using (MyLock.Lock(foo)) ...
Thank you, Barry, very much. That was exactly what I needed to know. I
had not heard of the Monitor class, but that looks to be exactly what
I needed.

I can't see how this would apply to try/catch. It's too bad that the
try/catch stuff has to go all the way to the OS level to stop from
executing code. Here's the document MS responded with for my feedback
(id 256733) on the topic: http://www.microsoft.com/msj/0197/ex...exception.aspx

Mar 15 '07 #5
Brian Gideon wrote:
On Mar 15, 12:30 pm, Barry Kelly <barry.j.ke...@gmail.comwrote:
Create an object that implements IDisposable, and use

using (MyLock.Lock(foo))
{

}

The C# team could have eliminated a keyword had they used that pattern
to begin with. It also would be less confusing for novice developers
who try to figure out the purpose of a Monitor.Pulse or Monitor.Wait
embedded in a lock block, not realizing that the lock keyword and the
Monitor class are intimately related. Why did they choose to create
the lock keyword anyway?
I read somewhere that AndersH regretted adding 'lock' after adding
'using'.

-- Barry

--
http://barrkel.blogspot.com/
Mar 15 '07 #6
not_a_commie wrote:
Barry Kelly wrote:
using (MyLock.Lock(foo)) ...

Thank you, Barry, very much. That was exactly what I needed to know. I
had not heard of the Monitor class, but that looks to be exactly what
I needed.

I can't see how this would apply to try/catch.
The second approach I listed, the delegate approach, can easily be
applied to try/catch. You pass the code as an anonymous delegate as an
argument.
It's too bad that the
try/catch stuff has to go all the way to the OS level to stop from
executing code.
I typically implement my event handlers inside anonymous delegates
passed to wrappers, as indicated in my grandparent post.

Alternatively, you can add a handler to either:

1) Application.ThreadException - for exceptions thrown during Windows
message processing, typically due to e.g. throwing an exception in a
control's event handler.

2) AppDomain.UnhandledException - for those exceptions that would
otherwise fall off the entire thread's stack.

-- Barry

--
http://barrkel.blogspot.com/
Mar 15 '07 #7
Brian Gideon <br*********@yahoo.comwrote:
The C# team could have eliminated a keyword had they used that pattern
to begin with. It also would be less confusing for novice developers
who try to figure out the purpose of a Monitor.Pulse or Monitor.Wait
embedded in a lock block, not realizing that the lock keyword and the
Monitor class are intimately related. Why did they choose to create
the lock keyword anyway?
Because they were copying Java ;)

Those following this thread may be interested in the locking classes
I've got in my utility library:

http://pobox.com/~skeet/csharp/miscu...e/locking.html

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Mar 15 '07 #8
Why did they choose to create
the lock keyword anyway?
To understand that one must first understand why we need a lower-case
"string" keyword.

Mar 15 '07 #9

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

Similar topics

1
by: leecho | last post by:
Hi, recently, i was assigned as a new dba for our system. I found that my statistic keep change from time to table. To look for the cause, i wanna to lock a table, means only allow user to...
0
by: Subhakar Burri | last post by:
------_=_NextPart_001_01C3555C.697B5893 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable Hi all, Is there a 'sleep' command in mysql? I'm trying to...
2
by: Bangalore | last post by:
Hi, Plz, clarify me , with the differences between advisory lock and mandatory locks. Thnanks in advance Bangalore.
0
by: davidmichaelkarr | last post by:
I'm using WebLogic Scripting Tool, which uses Jython, which uses Python 2.1. In a script I'm writing, executing a "cd()" always emits a newline to stdout. Is there a way to make it not emit that...
2
by: Roopesh | last post by:
Hi, In my mod_python project I am using mysql as the database. There is table card in which unique cards are stored. When a user request comes he has to get a unique card. In this situation I want...
9
by: kavallin | last post by:
I receives the following in the db2diag.log file many times / day : 2007-03-05-14.55.24.836553+060 E12415C457 LEVEL: Warning PID : 2785 TID : 1 PROC :...
5
by: Jennifer.Berube | last post by:
What is it exactly that makes application.lock such a bad thing? I just need a better explanation or link to a resource desribing it's downfall.
1
by: Andy Wynn | last post by:
I have written a windows service that will be used as a licensing server for a desktop application. I was planning to use a generic list to hold the license objects. For the majority of the time...
2
dlite922
by: dlite922 | last post by:
I'm no linux guru, but I get around the OS fine. I deployed a CentOS 5 server and with PHP 5, Apache 2.2 and MySQL 5 a couple of months ago. I noticed that apache and sendmail just go dead after a...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.