Error Creating a Map

671
4
Jump to solution
02-02-2018 10:39 AM
Highlighted
New Contributor

"This method or property must be called on the thread this object was created on.

I get this exception when creating a Map object either from an existing map or creating a new map.


Map map = null;
//ArcGIS.Desktop.Framework.Dialogs.MessageBox.Show("Open");
var mapProjectItems = Project.Current.GetItems<MapProjectItem>();
if (mapProjectItems.Count() > 0)
{
var mapProjectItem = mapProjectItems.First();
//FirstOrDefault(mpi => mpi.Name.Equals("World Map"));
map = mapProjectItem.GetMap();
ProApp.Panes.CreateMapPaneAsync(map);
}
else
{
map = MapFactory.Instance.CreateMap("World Map", ArcGIS.Core.CIM.MapType.Map, ArcGIS.Core.CIM.MapViewingMode.Map, Basemap.Terrain);
}

Mitch

Reply
0 Kudos
1 Solution

Accepted Solutions
Highlighted
Regular Contributor
4 Replies
Highlighted
Regular Contributor
Highlighted
Regular Contributor

How can this be marked as the correct answer when it does not answer the question? none of the Pro Snippiest show create a new map definition, add it to the current project, and make it the active map.

Reply
0 Kudos
Highlighted
by Esri Regular Contributor
Esri Regular Contributor

Hi Richard,

 When you click the link in the answer it will take you to a snippet that shows the "MapFactory.Instance.CreateMap" method you asked about in your original question.  The snippet shows that you have to call the CreateMap method from within the context of the MCT or main CIM thread using QueuedTask.Run.  Usually you can see the MCT requirement for any method by looking at the API help for the method or through intellisense (when you hover your mouse pointer over the method name in Visual Studio).  The requirement for MCT is listed in the help always like this:  "This method must be called on the MCT. Use QueuedTask.Run".  So the fix for your snippet would look like this:

protected override async void OnClick()
{
Map map = null;
//ArcGIS.Desktop.Framework.Dialogs.MessageBox.Show("Open");
var mapProjectItems = Project.Current.GetItems<MapProjectItem>();
await QueuedTask.Run(() =>
{
if (mapProjectItems.Count() > 0)
{
var mapProjectItem = mapProjectItems.First();
//FirstOrDefault(mpi => mpi.Name.Equals("World Map"));
map = mapProjectItem.GetMap();
ProApp.Panes.CreateMapPaneAsync(map);
}
else
{
map = MapFactory.Instance.CreateMap("World Map", ArcGIS.Core.CIM.MapType.Map, ArcGIS.Core.CIM.MapViewingMode.Map, Basemap.Terrain);
}
});


}

Reply
0 Kudos
Highlighted
Regular Contributor

I had the this same error, in my case I fixed the issue by making my method public.

public static async void LoadNewProject(CreateProjectSettings ps2)
{
//load the newly defined project; must be public to avoid threading errors!
try
{
Project thePrj = await Project.CreateAsync(ps2);
await Project.OpenAsync(thePrj.URI.ToString());
}
catch (Exception eX)
{
//do something
TrackErrorLibrary.reportError("SystemError", Project.Current.Name.ToString() + ". " + eX.Message + ". ");
}
}

Reply
0 Kudos