Error Creating a Map

3977
4
Jump to solution
02-02-2018 10:39 AM
MitchWolberg
New Contributor II

"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

1 Solution

Accepted Solutions
GKmieliauskas
Esri Regular Contributor
4 Replies
GKmieliauskas
Esri Regular Contributor
RichardDaniels
Occasional Contributor III

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.

Wolf
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);
    }
  });


}

0 Kudos
RichardDaniels
Occasional Contributor III

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 + ". ");
}
}

0 Kudos