Project Addin - TimeScaleValue.Value throwing ComException

E

Eric Hutzelman

I am getting somewhat random ComExceptions when trying to iterate through
some of Project's timescale data using the following code. According to the
exception, the culprit is the TimeScaleValue.Value property:

TimeScaleValues timeScaleValues = assignment.TimeScaleData(startDate,
endDate, PjAssignmentTimescaledData.pjAssignmentTimescaledActualWork,
PjTimescaleUnit.pjTimescaleDays, 1);

foreach (TimeScaleValue tsValue in timeScaleValues)
{
// ComException thrown by tsValue.Value at random times
object value = tsValue.Value;
}

Searching the web, I came upon a couple posts of the problem, here is one
which describes the problem I am seeing exactly:

"I have been working on some .net code to enter timephased data into project,
via a managed addin, and have been getting Com interop errors randomly
interspersed (COMPlusExceptionCode -532459699, COMException -2146827284)

The code is reasonably simple, and using exactly the same test data and
conditions, it might fail on the 31st, or it might fail on the 12, or the
10th. With exactly the same data... The below code fails randomly on the
"timeScaleValue.Value =". When the error occurs, the timescale.value object
does not get set properly after the call to assignment.TimeScaleData(). The
Value object is a ComInteropException class object rather than a String or
Double, as it failed getting the data.

TimeScaleValues timeScaleValues = assignment.TimeScaleData(
date,
date.AddDays(1),
PjAssignmentTimescaledData.pjAssignmentTimescaledActualWork,
PjTimescaleUnit.pjTimescaleDays,
1);
foreach (TimeScaleValue timeScaleValue in timeScaleValues)
{
timeScaleValue.Value = (double)(hours * 60.0);
break;
}

I've tried a lot of variants of this code, like not setting the value, and
deleting the timescalevalue and just Add()-ing a new one, and But same
deal - random cominterop errors."

A second post I found seems to identify a solution, but it didn't work for
me. This was posted by Brook Miles of Replicon, Inc.:

"I recently ran up against this problem and found that while there were
several posts about it spanning years, nobody seems to have posted a
solution.

Calling TimeScaleData returns a TimeScaleValues collection containing
TimeScaleValue items. When iterating through the returned list in C#
or VB.NET and accessing the Value member of each item, a COMException
will be thrown on the 10th item.

Aparently there is an internal resource limit in Project which
prevents holding references to more than 9 TimeScaleValue objects at
the same time, and the .NET garbage collector is not clearing up the
references fast enough.

A work-around for this is to force the garbage collector to clean up
its references after each execution of the loop. Placing a call to
System.GC.Collect() at the end of the loop appears to solve the
problem.

This is not an elegant solution, but it does work. Thanks to Shaun
Ivory at Microsoft for assisting me in diagnosing this problem."

I am still seeing the same errors after putting in the call to GC.Collect(),
so I am trying to figure out a solution for this. This issue seems to be
documented back to at least 2003.

Thanks for any insight,
Eric
 
E

Eric Hutzelman

Got it sorted out, so just an FYI in case anyone else runs into this. It
does indeed seem to be fixed by the GC, but you also need to put in this line
after the GC.Collect() to allow the GC time to do its thing:

System.GC.WaitForPendingFinalizers();
 

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