473,729 Members | 2,150 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to determine whether an object is a VALID COM object

I have a method Marshal.IsComOb ject(...) that returns TRUE if an
object is a COM object.
I have an object of Excel.Worksheet that I released using
Marshal.Release ComObject(...). I want to iterate over a collection of
Excel.Worksheet and find out which is disposed.
Currently the workaround for me (bad one) is

try
{
string name = worksheet.Name;
}
catch(Exception e)
{
// this one was release as I cannot access it
worksheet = null;
}

Is there any other method through which I can check for a VALID(not
released) COM object?

Regards,
Jun 27 '08 #1
3 5614
abhimanyu wrote:
I have a method Marshal.IsComOb ject(...) that returns TRUE if an
object is a COM object.
I have an object of Excel.Worksheet that I released using
Marshal.Release ComObject(...). I want to iterate over a collection of
Excel.Worksheet and find out which is disposed.
There's no way to determine whether an object is disposed other than using
it and getting an ObjectDisposedE xception. And that's actually the *good*
scenario. If you were working with pure COM, you'd have no way at all of
knowing whether an interface pointer was valid, and you'd likely just get a
crash if you use an invalid one.

You must keep track of when an object was disposed yourself. In general, do
not dispose objects until you're certain (or have made certain) that nobody
will use them anymore, then you can omit keeping track.
Currently the workaround for me (bad one) is

try
{
string name = worksheet.Name;
}
catch(Exception e)
Catch specific exception types, not Exception. The call to worksheet.Name
could fail for multiple reasons, not all of which should simply be silently
discarded.
{
// this one was release as I cannot access it
worksheet = null;
}

Is there any other method through which I can check for a VALID(not
released) COM object?
If you know you released the object, why do you need to check for it here?
Somewhere inbetween you discarded information or failed to propagate it.

Why don't you set the reference to null when you release it, rather than
when you find out it was released? If the worksheet is in a collection, why
don't you remove it from the collection?

--
J.
http://symbolsprose.blogspot.com
Jun 27 '08 #2
Actually, If I am keeping a collections of Sheets in a Dictionary<T>.
If I delete a Excel worksheet manually in Excel, the reference remains
in my Dictionary and when I try to use it, it throws and exception.

There may be other workaround to this problem by following a certain
pattern, But what I wanted to know was whether there is a method that
can avoid that throw.

Regards,
Jun 27 '08 #3
abhimanyu wrote:
Actually, If I am keeping a collections of Sheets in a Dictionary<T>.
If I delete a Excel worksheet manually in Excel, the reference remains
in my Dictionary and when I try to use it, it throws and exception.
I don't know the details of the Excel automation model, but I'd be surprised
if there was no way to detect that the sheet was deleted. There is probably
an event handler for that.
There may be other workaround to this problem by following a certain
pattern, But what I wanted to know was whether there is a method that
can avoid that throw.
To the best of my knowledge, there is not, and even relying on an exception
being generated here is iffy. Using an object when you're not sure if it's
still valid is a bad approach, because you're relying on the COM server to
detect the error for you.

A better way would be to *not* keep the collection of Sheets yourself, but
rely on Excel's collections instead, in combination with some unique sheet
identifier if necessary for maintaining your own data. Do not use a Sheet
without looking up if it still exists.

--
J.
http://symbolsprose.blogspot.com
Jun 27 '08 #4

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

Similar topics

2
4341
by: OvErboRed | last post by:
Hi, I'm trying to determine whether a given URL exists. I'm new to Python but I think that urllib is the tool for the job. However, if I give it a non-existent file, it simply returns the 404 page. Aside from grepping this for '404', is there a better way to do this? (Preferrably, there is a solution that can be applied to both HTTP and FTP.) Thanks in advance.
7
12928
by: JT | last post by:
is there a way to determine if a form object actually exists? if i use the following syntax: varTextField = Request.Form("txtFormField") then varTextField = "" which gives the same result whether the input field "txtFormField" doesn't exist or it does exist but it was left empty why can't i use something like?
1
8963
by: Carolyn Speakman | last post by:
Hi, I'm trying to amend an intranet .asp page but keep getting this error. The error only occurs when I pull the WHOLE intranet off the server and try to run it locally. The error isn't there when the page is run off the intranet server. I'm using IIS 5, I.E. 6 and WinXP Pro. Here's the code it doesn't like:
16
6197
by: Donjuan | last post by:
Hi all I have trouble with tracking whether my image file is loaded. i use DHTML to change my image. HERE is the code: <img name="someimage" src="1.jpg" onclick="document.all.someimage.src='someimage.jpg"> but how can i determine whether "someimage.jpg" is loaded? and can i get the download percent of a file? Thanks in advance
2
1530
by: Shravan | last post by:
Hi, In my application I need to determine whether Excel is installed on my system or not. Can anybody tell me how to check that. Thanks, Shravan.
0
1118
by: hoenes1 | last post by:
How can I determine whether a download has been cancelled by the client? I'm sending the file from an aspx page in chunks of 4K. When the user presses "Cancel" in his download dialog, the next call of Response.Flush() hangs and resets my Session Object after about a minute. The Response.IsClientConnected property is still true after the user pressed "Cancel". Is there a workaround for this problem? The Code is: // fs --> FileStream //...
25
14499
by: _DD | last post by:
I'd like to include a 'Test Connection' button in an app, for testing validity of a SQL connection string. I'd prefer to keep the timeout low. What is the conventional way of doing this?
1
2562
by: topramen | last post by:
does any one here know of a good way to to determine whether or not a given path is a directory (e.g., "c:\mydir") or a file (e.g., "c:\mydir \myfile.txt")? i started attacking this problem by using filesystemobject to check whether or not a directory existed for the path. if so, it was a directory path. if not, i checked to see if a file existed for it. if so, it was a file path. this code is shown below: ...
9
3781
by: Mark Berry | last post by:
Hi, How can I determine whether an object is derived from another object? My specific example is that I have a CustomError class with several specific error types that derive from it (CustomBuinessError, CustomTechnicalError, etc.). The CustomError base class has some extra fields that I want to log or display when an error is handled. I know I can check for exception types in Catch blocks. But what about in
0
8761
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
9426
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
9280
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
9142
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8144
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
6722
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
4795
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3238
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
2677
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.