Sunday, 13 March 2016

arcobjects - Passing WorkspaceName to another thread for opening?


My winforms app gets an IWorkspaceName from an IDatasetName via the GxObject selected in a GxDialog. I want to pass it across to a worker thread for opening.


I know how to write multithreaded arcobjects code - particularly the need to create the workspace factory class on the worker thread but my app makes no assumptions as to the type of workspace factory. I've considered passing it as a serialized xml string, but the documentation seems strangely vague on this topic. Does anyone have code to do this?


On preview,


I'm asking much the same as this question. Has anyone cracked the xml serialization / deserialization? It seems the simplest way to go or is there a better way?



Answer



See my example code below, it will enable you to deserialize instance of IWorkspaceName on the other thread from the XML produced by the serialization method.


It can be easily modified to allow serialization and deserialization of any ArcObject which implements ESRI.ArcGIS.esriSystem.IXMLSerialize.


    private static string GetWorkspaceNameAsXmlString(IWorkspaceName workspaceName)
{

if (workspaceName == null) throw new ArgumentNullException("workspaceName");

var xmlStream = new XMLStreamClass();
var xmlWriter = new XMLWriterClass();
var xmlSerializer = new XMLSerializerClass();

xmlWriter.WriteTo(xmlStream);
xmlSerializer.WriteObject(xmlWriter, null, null, string.Empty, string.Empty, workspaceName);

return xmlStream.SaveToString();

}

private static IWorkspaceName GetWorkspaceNameFromXmlString(string xml)
{
if (xml == null) throw new ArgumentNullException("xml");

var xmlStream = new XMLStreamClass();
var xmlReader = new XMLReaderClass();
var xmlSerializer = new XMLSerializerClass();


xmlStream.LoadFromString(xml);
xmlReader.ReadFrom(xmlStream);

return xmlSerializer.ReadObject(xmlReader, null, null) as IWorkspaceName;
}

No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...