To represent a graphic moving along with mouse cursor, I am adding a point graphic on mouse move and clear it when mouse move is triggered at a new location and a graphic for new location. But the graphics never gets cleared. They stay on map. Here is the code snippet
private IDisposable PtGraphic;
protected override void OnToolMouseMove(MapViewMouseEventArgs e)
{
QueuedTask.Run(() =>
{
var pt = ActiveMapView.ClientToMap(e.ClientPoint);
ClearGraphic(ref PtGraphic);
PtGraphic = AddGraphicToMap(pt, ColorFactory.Instance.RedRGB, 3, SimpleMarkerStyle.Circle);
});
base.OnToolMouseMove(e);
}
private void ClearGraphic(ref IDisposable graphic)
{
if (graphic != null)
graphic.Dispose();
graphic = null;
}
public static async Task<IDisposable> AddGraphicToMap(Geometry geom, CIMColor color,
double size = 1.0, SimpleMarkerStyle markerStyle = SimpleMarkerStyle.Rectangle)
{
///Build Symbology
return MapView.Active.AddOverlay(geom, symbol);
}
Am I missing something ?
Appreciate any help.
Solved! Go to Solution.
I believe there is an error in this code as AddGraphicToMap returns a Task<IDisposable> which is apparently being converted to an IDisposable (PtGraphic) when what you want to do is await the Task to extract the IDisposable that results, you can do this by marking the lambda you pass to QueuedTask.Run as 'async' and then add 'await' on the call to AddGraphicToMap. There were some timing related bugs in this area installed during 2.6 development.
Also I believe you should consider using DelayedInvoker https://pro.arcgis.com/en/pro-app/sdk/api-reference/#topic10949.html so you don't handle every mouse move event but throttle to handle mouse move events as fast as they can be processed.
I believe there is an error in this code as AddGraphicToMap returns a Task<IDisposable> which is apparently being converted to an IDisposable (PtGraphic) when what you want to do is await the Task to extract the IDisposable that results, you can do this by marking the lambda you pass to QueuedTask.Run as 'async' and then add 'await' on the call to AddGraphicToMap. There were some timing related bugs in this area installed during 2.6 development.
Also I believe you should consider using DelayedInvoker https://pro.arcgis.com/en/pro-app/sdk/api-reference/#topic10949.html so you don't handle every mouse move event but throttle to handle mouse move events as fast as they can be processed.
Thanks John Jones. After looking at your answer, I changed the Add Graphic method to a non-async method and just returned IDisposable instead of Task<IDisposable>. It's working good for now, but I observe a slight delay, I will take a look at the DelayedInvoker. Cheers!
John,
I realize this is an old posting. However, I was hoping you could tell me how to implement DelayedInvoker. I can't find any examples or documentation on it.
Thank you.