Your server administrator has limited the number of items you can

S

spottedmahn

I'm receiving the following COMException:

"Your server administrator has limited the number of items you can open
simultaneously. Try closing messages you have opened or removing attachments
and images from unsent messages you are composing."

when I try to iterate thru a list of Contact Items.

I've read a few posts on this same error message but I'm still unsure how of
how to properly fix it.

Here is my code:

List<ContactItem> Results = new List<ContactItem>();

Outlook.Items Contacts = this.WcmsContactsMapiFolder.Items;

Outlook.ContactItem Contact = (Outlook.ContactItem)
Contacts.GetFirst();

do
{
ContactItem NewCont =
ContactItem.FromOutlookContactItem(Contact, this.SifApp);

Results.Add(NewCont);

Marshal.ReleaseComObject(Contact);

Contact = (Outlook.ContactItem)Contacts.GetNext();

} while (Contact != null);

return Results;

I've read posts that say you must call " Marshal.ReleaseComObject()" but
that doesn't appear to be closing the connection.

I've read posts that say to increase the simultaneous connection limit but
that doesn't see right to me. I'm able to view the contact list in Outlook
so it is reading it somehow.
 
K

Ken Slovak - [MVP - Outlook]

Declare NewCont outside the loop so you aren't creating an instance of it
each pass through the loop. Set both contact objects to null, if that's not
enough to prevent the error then call Marshal.ReleaseComObject() on them. If
that's not enough then call the GC and WaitForPendingFinalizers(). Do it in
steps so you don't add more handling than the minimum needed. Using
ReleaseComObject() and the garbage collection is expensive.
 
S

spottedmahn

Hi Ken, thanks for the reply.

The point of the loop is to convert Outlook Contacts objects to My Custom
Contact Objects so it doesn't make sense to declare NewCont outside the loop.
Maybe I'm mis-understanding you?

I've seen similar suggestions in other forum posts and it seems like a cross
your fingers and hope solution which doesn't make me feel very comfortable.

It seems like there should be a reliable way of "closing" objects that you
open. Am I missing something?

Thanks,
Mike D.
 
K

Ken Slovak - [MVP - Outlook]

Yes, if you don't follow the advice you've seen from me and other posters
you are missing something. If you follow the advice you won't have those
problems.

Declaring the NewCont object outside the loop creates only 1 of those
objects. You just set it and reset it inside the loop. If you declare it
inside the loop and the loop executes 200 times you just created 200 of
those objects.

You can still do what you want, and then have to use
Marshal.ReleaseComObject() on each object plus setting each object to null,
then calling GC and WaitForPendingFinalizers on each pass through the loop.
That will also work, but the performance of loop execution will suffer a
lot. What I recommended is a balance of getting the loop to work and having
it work as fast as possible.
 
S

spottedmahn

Hi Ken, thanks again for the reply.

I'm still not clear on your analysis of declaring NewCont outside the loop.
Whether I put NewCont inside or outside the loop N objects will be created.

The point of the loop is to take a Outlook Contact and create a Custom
Contact Object. Therefore for every Outlook Contact object there will be one
Custom Contact Object.

So if I have 200 Outlook Contacts in a MapiFolder the above code would
return a List<CustomContactObject> whose count is 200. Make sense?

As for setting each object to null I'm unclear on how that would effect
anything. With each iteration of the loop the variables are re-assigned to
other objects.

So on Pass 1 Contact = X, then on Pass 2 Contact = Y. Nothing points to X
anymore.

I'm not trying to be combative I'm just trying to understand.

Thanks for you input,
Mike D.
 
K

Ken Slovak - [MVP - Outlook]

If you declare an object outside the loop

Outlook.ContactItem NewCont = null;

and then instantiate instances of it within the loop you are only creating
one object. You are assigning an instance of that object each time through
the loop, then you set it to null/release. Next time through you are just
instantiating another instance of that object.

If you declare the object inside the loop you create one object each pass
through the loop, then instantiate it then release it.

Those 2 are not identical scenarios.

If you don't release the objects then they remain in memory until some
undetermined time in the future when the garbage collector finally runs
after the items go out of scope. The RPC channels remain committed until
those objects are released. Therefore you get the errors you described.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top