473,804 Members | 2,277 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2012
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(fo o))
{
}

.... 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(_o bj);
// 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(deleg ate
{
// 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(fo o))
{

}
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(fo o))
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, ReaderWriterLoc k, etc.

-- Barry

--
http://barrkel.blogspot.com/
Mar 15 '07 #4
Barry Kelly wrote:
using (MyLock.Lock(fo o)) ...
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(fo o))
{

}

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(fo o)) ...

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.Thr eadException - for exceptions thrown during Windows
message processing, typically due to e.g. throwing an exception in a
control's event handler.

2) AppDomain.Unhan dledException - 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*********@ya hoo.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.co m>
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
4579
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 insert. Others than that, noway. Can i use the Lock table command? or others comand? p/s: we are using 9i client and 6i developer regards,
0
2671
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 take a backup of = MySQL instance from a ksh script, and I want to lock all the tables = before I take a back of all datafiles. So, once I issue the lock tables =
2
4725
by: Bangalore | last post by:
Hi, Plz, clarify me , with the differences between advisory lock and mandatory locks. Thnanks in advance Bangalore.
0
1269
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 newline on the "cd()" command (and others like it)?
2
3433
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 to use LOCK with which I can prevent other users accessing the table. I tried excuting "LOCK" command of mysql through python code, but it is not locking the database. Any ideas why this isn't working and how can I do the same. //python code...
9
15569
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 : db2agent (dbname) INSTANCE: db2inst1 NODE : 000 DB : dbname APPHDL : 0-946 APPID: *LOCAL.db2inst1.070305135434 FUNCTION: DB2 UDB, data management, sqldEscalateLocks, probe:3 MESSAGE : ADM5502W The escalation of...
5
2453
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
1330
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 the service will see a small about of traffic, since the number of licenses that will need to be tracked in less than 12. I was wondering if using Lock is safe and appropriate to use on generic lists, or if I'm looking in the wrong place all...
2
1962
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 few weeks (2 times that it has happened so far, and I'm guessing it will happen again). Had to kill the services and restart them. I have no clue how to diagnose this. Can someone point me to a direction, how to log it when it happens again (if its...
0
10599
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...
1
10347
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9173
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
7635
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
6863
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
5531
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
4308
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
2
3832
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3001
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.