473,403 Members | 2,183 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,403 software developers and data experts.

DirectoryEntry Disposal Test

I am in a situation in which I am recieving a DirectoryEntry that may or
may not be already be disposed. I need a very fast, (from a code point
of view not necissarily a coding point of view), way of testing if it
has been disposed.

Currently I have:

try
{
string Junk MyDirectoryEntry.Guid.ToString();
}
catch
{
// it has been disposed of, put logic here
}

This is so painfully slow. The 1 line in the try take .00450 seconds and
that of course is not acceptable. Does anyone have a nicer way of
testing for this condition?

-Cam
Nov 16 '05 #1
7 2048
Well, DirectoryEntry has a private bool field called disposed. You could use
reflection to get that out. I'd imagine that'd be faster than throwing an
exception.

And, AFAIK, if the property cache is filled, then getting the guid might not
throw an exception.

-mike
MVP

"cameron" <ca****************@appdepot.com> wrote in message
news:eg**************@TK2MSFTNGP11.phx.gbl...
I am in a situation in which I am recieving a DirectoryEntry that may or
may not be already be disposed. I need a very fast, (from a code point of
view not necissarily a coding point of view), way of testing if it has been
disposed.

Currently I have:

try
{
string Junk MyDirectoryEntry.Guid.ToString();
}
catch
{
// it has been disposed of, put logic here
}

This is so painfully slow. The 1 line in the try take .00450 seconds and
that of course is not acceptable. Does anyone have a nicer way of testing
for this condition?

-Cam


Nov 16 '05 #2
cameron wrote:
I am in a situation in which I am recieving a DirectoryEntry that may or
may not be already be disposed. I need a very fast, (from a code point
of view not necissarily a coding point of view), way of testing if it
has been disposed.

Currently I have:

try
{
string Junk MyDirectoryEntry.Guid.ToString();
}
catch
{
// it has been disposed of, put logic here
}

This is so painfully slow. The 1 line in the try take .00450 seconds and
that of course is not acceptable. Does anyone have a nicer way of
testing for this condition?


Looking at the Directory Entry class with Lutz Roeder's Reflector, it
appears that the Guid property doesn't test for the object being
disposed. In other words, your method won't give you the results you
want regardless of its speed.

The Name property does check for the object being disposed (indirectly).
I have no idea how fast or slow it is. Note that in the case where
the object is disposed, the test will be slow because of the exception.
Hopefully for non-disposed objects, the Name property will be pretty quick.

AS far as the suggestion for using reflection to test the private
'disposed' field, that should work, but I'm guessing that it's not very
fast, and depending on how your application is deployed you might have
problems with code access security.

--
mikeb
Nov 16 '05 #3
> Looking at the Directory Entry class with Lutz Roeder's Reflector, it
appears that the Guid property doesn't test for the object being disposed.
In other words, your method won't give you the results you want regardless
of its speed.

The Name property does check for the object being disposed (indirectly). I
have no idea how fast or slow it is. Note that in the case where the
object is disposed, the test will be slow because of the exception.
Hopefully for non-disposed objects, the Name property will be pretty
quick.
Well, the Guid does get it checked, again indirectly. Essentially every
property will, if it's not already cached. Calling a method that definately
forces it (such as RefreshCache or likewise) will guarantee it.
AS far as the suggestion for using reflection to test the private
'disposed' field, that should work, but I'm guessing that it's not very
fast, and depending on how your application is deployed you might have
problems with code access security.


Yea, it's slow, but I doubt it's slower than catching an exception, at least
in the fail case (I dont know the average test case, so it's hard to know
what to optimize for). If one more person cares, I'll go do some benchmarks
:).

Also, reflecting on a private field is a poor, breakable, hack. Just thought
I'd mention that.

-mike
MVP

Nov 16 '05 #4
Michael Giagnocavo [MVP] wrote:

Well, the Guid does get it checked, again indirectly. Essentially every
property will, if it's not already cached. Calling a method that
definately forces it (such as RefreshCache or likewise) will guarantee it.

On second look, it does appear that the Guid property does potentially
call Bind() (the private method that actually checks whether the object
is disposed). But it looks like there is some chance that Bind() might
not be called (as you say, depending on whether the information is
cached). The Name property unconditionally calls the Bind() method.
Also, reflecting on a private field is a poor, breakable, hack. Just
thought I'd mention that.


Depending on the undocumented behavior of the properties is also
breakable (although accessing a property on a disposed object should
cause an exception).

The original poster might consider simply wrapping the entire processing
of the DirectoryEntry in a try/catch, and just do whatever processing he
intends to do, with no special test for a disposed object. Let the big
try/catch deal with the disposed objects; that way the processing only
takes a hit when called with a reference to an already disposed
DirectoryEntry.

--
mikeb
Nov 16 '05 #5
> The original poster might consider simply wrapping the entire processing
of the DirectoryEntry in a try/catch, and just do whatever processing he
intends to do, with no special test for a disposed object. Let the big
try/catch deal with the disposed objects; that way the processing only
takes a hit when called with a reference to an already disposed
DirectoryEntry.


Or register references to the disposed objects in some kind of collection.
But then again, if he has access to them when they are getting disposed,
then he might be able to just redesign this part of the app :).

-mike
MVP

Nov 16 '05 #6
Basically Is see two options:
1) Implement the DirectoryEntry.Disposed eventhandler, in which you set a
flag ( f.i IsDisposed) you can check before using the DirectoryEntry object.
2) Put all your processing in a using block.

Willy.
"cameron" <ca****************@appdepot.com> wrote in message
news:eg**************@TK2MSFTNGP11.phx.gbl...
I am in a situation in which I am recieving a DirectoryEntry that may or
may not be already be disposed. I need a very fast, (from a code point of
view not necissarily a coding point of view), way of testing if it has been
disposed.

Currently I have:

try
{
string Junk MyDirectoryEntry.Guid.ToString();
}
catch
{
// it has been disposed of, put logic here
}

This is so painfully slow. The 1 line in the try take .00450 seconds and
that of course is not acceptable. Does anyone have a nicer way of testing
for this condition?

-Cam

Nov 16 '05 #7
The "real" cache is contained in the unmanaged code portion (ADSI COM
object) as an unmanaged memory resource, but as the DirectoryEntry object is
disposed off, so is the reference to the underlying COM object reference.
Now the (managed) property data only reflects a part of the cached ADSI
data, so it wouldn't be wise to access this data once the object is
disposed.
Therefore, System.ObjectDisposedException is thrown on you when you try to
access the DirectoryEntry object reference.

Willy.
"Michael Giagnocavo [MVP]" <mg*******@Atrevido.net> wrote in message
news:Ox**************@tk2msftngp13.phx.gbl...
Well, DirectoryEntry has a private bool field called disposed. You could
use reflection to get that out. I'd imagine that'd be faster than throwing
an exception.

And, AFAIK, if the property cache is filled, then getting the guid might
not throw an exception.

-mike
MVP

Nov 16 '05 #8

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

Similar topics

2
by: Jessica | last post by:
When I create a single LDAP ActiveDirectory user and use DirectoryEntry.Invoke("SetPassword"...), the user is created and the password is set with no problems. However, when I try to add more...
7
by: cameron | last post by:
I am in a situation in which I am recieving a DirectoryEntry that may or may not be already be disposed. I need a very fast, (from a code point of view not necissarily a coding point of view), way...
2
by: Jay | last post by:
Please excuse the double post if your reader shows this as a new and as a reply. I can only use the web interface and it puts my reply 34 pages in. I am getting an error while trying to do a...
2
by: AbdSol | last post by:
I wanted to know why the output is different & also like to know if C# i can get the user name using DirectoryEntry > find. C# String myADSPath = ("WinNT://"+...
1
by: kenguil | last post by:
I'm hoping that this is the right group to post this to and that someone will have an answer to this. Environment: 1) Laptop joined to domain "foo.com", logged in using cached credentials 2)...
0
by: Mik | last post by:
Hi I am struggling to make a Web site/Web Directory copy work. I am using the System.DirectoryServices to connect to my localhost machine. There I have two web sites: Mysite1 (siteno 1) and...
5
by: ABSMunkee | last post by:
I have a vb.net dll that has two functions: one allows a user to change their password in AD, the second allows the user to view their distinguishedname (based on their samaccountname). Both bind...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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...
0
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,...
0
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
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,...
0
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...

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.