Read part Property occurs share conflict

Hello,

 

I use SolidEdgeFileProperties.Propertysets read file properties from a part file when I used SolidEdgeFileProperties.Propertysets write some file properties into a part file.

There is an error,share conflict occured. This will be ok in a undetermined time(maybe 1 minute) later.

 

Here is my code.Please help me to check if this problem can be solved,then I can read out the properties when I wrote into them.

private StringBuilder GetFileProperty(string filename)
        {
            PropertySets propertySets = null;
            SolidEdgeFileProperties.Properties properties = null;
            Property property = null;
            StringBuilder sb = new StringBuilder();
            string format2 = "{0} = {1}";
            try
            {
                // Create new instance of the PropertySets object
                propertySets = new SolidEdgeFileProperties.PropertySets();
                // Open a file
                propertySets.Open(tbFileName.Text, true);
                // Example: Loop through all properties
                // Note that indexes are zero based
                for (int i = 0; i < propertySets.Count; i++)
                {
                    properties = (SolidEdgeFileProperties.Properties)propertySets[i];
                    // Note that indexes are zero based
                    for (int j = 0; j < properties.Count; j++)
                    {
                        property = (SolidEdgeFileProperties.Property)properties[j];
                        try
                        {
                            sb.AppendLine(string.Format(format2, property.Name, property.Value));
                        }
                        catch
                        {
                            sb.AppendLine(string.Format(format2, property.Name, "ERROR"));
                        }
                    }
                }
            }
            catch (System.Exception ex)
            {
                sb.AppendLine(ex.Message);
            }
            finally
            {
                if (property != null)
                {
                    Marshal.ReleaseComObject(property);
                    property = null;
                }
                if (properties != null)
                {
                    Marshal.ReleaseComObject(properties);
                    properties = null;
                }
                if (propertySets != null)
                {
                    propertySets.Close();
                    Marshal.ReleaseComObject(propertySets);
                }
            }
            return sb;
        }

 private void WriteProperty()
        {
            PropertySets propertySets = null;
            SolidEdgeFileProperties.Properties properties = null;
            Property property = null;
            try
            {
                // Create new instance of the PropertySets object
                propertySets = new SolidEdgeFileProperties.PropertySets();
                // Open a file
                propertySets.Open(tbFileName.Text, false);
                // Get reference to Custom Property Set
                properties = (SolidEdgeFileProperties.Properties)
                propertySets["custom"];
                // Add custom file properties
                property = (SolidEdgeFileProperties.Property)
                properties.Add("My String", "Hello");
                property = (SolidEdgeFileProperties.Property)
                properties.Add("My Integer", tbProperty.Text);
                property = (SolidEdgeFileProperties.Property)
                properties.Add("My Date", DateTime.Now);
                property = (SolidEdgeFileProperties.Property)
                properties.Add("My Boolean", true);
                // Delete last custom property
                //property.Delete();
                // Save changes
                propertySets.Save();

                rtbResult.Text = "Done";
            }
            catch (System.Exception ex)
            {
                rtbResult.Text = ex.Message;
            }
            finally
            {
                if (property != null)
                {
                    Marshal.ReleaseComObject(property);
                    property = null;
                }
                if (properties != null)
                {

                    Marshal.ReleaseComObject(properties);
                    properties = null;
                }
                if (propertySets != null)
                {
                    propertySets.Close();
                    Marshal.ReleaseComObject(propertySets);
                }
            }
        }

 

Thanks in advance!

 

Allen

 

Posted by: Allen
Post date: 9/30/2012 9:03:52 AM

3 REPLIES

Read part Property occurs share conflict

What version of windows are you using?

 

Posted by: Terry Tyson
Post date: 10/3/2012 7:38:56 AM

Read part Property occurs share conflict

It sounds like this may be another one of those App.DoIdle()/GC.Collect() issues. Both SE and Windows must "release" the file before it can be saved. Not sure how this plays out when you access the file directly. Just call GC.Collect() and see if that allows you to save the changes.

 

Posted by: Terry Tyson
Post date: 10/8/2012 11:56:47 AM

Read part Property occurs share conflict

Looking at your code, I can clearly see where things are going wrong for you. Don't feel bad though, it happens to all of us as it is quite difficult to accurately track every COM object you touch and release it accordingly. While GC.Collect() may seemingly fix your issue, the mere fact that you're having to call it lets us know that something else is wrong. I could go off on a tangent about the whole topic but I'll spare you (for now).

 

Interestingly enough, I found a really neat solution to the whole RCW not getting ReleaseComObject() called. I plan to write an article\blog about it because I think it's a viable solution, especially when automating Solid Edge. Start by reading the article http://msdn.microsoft.com/en-us/magazine/cc163316.aspx]CLR Inside Out: Managing Object Lifetime. I don't expect you to necessarily understand all of the details but buried in the article is a link to the http://codeplex.com/lifetimescope]LifeTimeScope.net Helper Classes on CodePlex. The following snippet is some proof-of-concept code I was playing around with over the weekend to see if this thing actually worked. Well, it does and I'm quite fond of the solution. With the help of the LifeTimeScope class, we can easily define a "box" that the RCW's live in and ultimately die in as the LifeTimeScope gets disposed.

 

 

using System;
using System.Collections.Generic;
using System.Text;

namespace SolidEdgeFilePropertiesTest
{
    class Program
    {
        [STAThread]
        static void Main(string[] args)
        {
            string filename = @"C:\Part1.par";

            TestWithLifeTimeScope(filename);
            TestWithLifeTimeScope(filename);
            
            TestWithoutLifeTimeScope(filename);
            TestWithoutLifeTimeScope(filename);
        }

        static void TestWithLifeTimeScope(string filename)
        {
            SolidEdgeFileProperties.IJPropertySets propertySets = null;
            SolidEdgeFileProperties.IJProperties properties = null;
            SolidEdgeFileProperties.IJProperty property = null;

            using (new LifeTimeScope())
            {
                try
                {
                    propertySets = new SolidEdgeFileProperties.PropertySetsClass().AddRef();
                    propertySets.Open(filename, false);

                    for (int i = 0; i < propertySets.Count; i++)
                    {
                        properties = (SolidEdgeFileProperties.IJProperties)propertySets[i].AddRef();
                        for (int j = 0; j < properties.Count; j++)
                        {
                            property = (SolidEdgeFileProperties.IJProperty)properties[j].AddRef();
                            Console.WriteLine(String.Format("{0}.{1}", properties.Name, property.Name));
                        }
                    }

                    propertySets.Save();
                }
                catch (System.Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
                finally
                {
                    propertySets.Close();
                }
            }
        }

        static void TestWithoutLifeTimeScope(string filename)
        {
            SolidEdgeFileProperties.IJPropertySets propertySets = null;
            SolidEdgeFileProperties.IJProperties properties = null;
            SolidEdgeFileProperties.IJProperty property = null;

            try
            {
                propertySets = new SolidEdgeFileProperties.PropertySetsClass();

                // Second call will fail because we're not cleaning up every RCW from last call!
                propertySets.Open(filename, false);

                for (int i = 0; i < propertySets.Count; i++)
                {
                    properties = (SolidEdgeFileProperties.IJProperties)propertySets[i];
                    for (int j = 0; j < properties.Count; j++)
                    {
                        property = (SolidEdgeFileProperties.IJProperty)properties[j];
                        Console.WriteLine(String.Format("{0}.{1}", properties.Name, property.Name));
                    }
                }

                propertySets.Save();
            }
            catch (System.Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            finally
            {
                if (property != null)
                {
                    System.Runtime.InteropServices.Marshal.ReleaseComObject(property);
                }
                if (properties != null)
                {
                    System.Runtime.InteropServices.Marshal.ReleaseComObject(properties);
                }
                if (propertySets != null)
                {
                    propertySets.Close();
                    System.Runtime.InteropServices.Marshal.ReleaseComObject(propertySets);
                }
            }
        }
    }
}

 

Posted by: Jason Newell
Post date: 10/15/2012 12:33:15 AM