Select to view content in your preferred language

Zoom to Layer refresh not working

3683
7
Jump to solution
11-16-2013 12:43 PM
DariusDarius
Emerging Contributor
Hi, what im trying to do is to zoom 2 times, with 3 seconds pause between zooms.
public void ZoomToLayerInTOC(ESRI.ArcGIS.ArcMapUI.IMxDocument mxDocument)         {             if (mxDocument == null)             {                 return;             }             ESRI.ArcGIS.Carto.IActiveView activeView = mxDocument.ActiveView;              // Get the TOC             ESRI.ArcGIS.ArcMapUI.IContentsView IContentsView = mxDocument.CurrentContentsView;             ESRI.ArcGIS.Geometry.IEnvelope envelope = activeView.Extent;            // ESRI.ArcGIS.Geometry.IEnvelope envelope = activeView.Extent;             envelope.Expand(0.75, 0.75, true);             activeView.Extent = envelope;             activeView.Refresh();             System.Threading.Thread.Sleep(3000);             envelope.Expand(0.75, 0.75, true);             activeView.Extent = envelope;             activeView.Refresh();             System.Threading.Thread.Sleep(3000);             // Get the selected layer             System.Object selectedItem = IContentsView.SelectedItem;             if (!(selectedItem is ESRI.ArcGIS.Carto.ILayer))             {                 return;             }             ESRI.ArcGIS.Carto.ILayer layer = selectedItem as ESRI.ArcGIS.Carto.ILayer;               // Zoom to the extent of the layer and refresh the map             activeView.Extent = layer.AreaOfInterest;             activeView.Refresh();         }


And after clicking button i get zoomed in after 6 seconds. Like there's no refresh after first zoom, although map scrollbars gets smaller.
0 Kudos
1 Solution

Accepted Solutions
JeffMatson
Frequent Contributor
Thanks for replying. Well im not really sure what am I supposed to do. I mean I don't know how. By Separate thread you mean create a function? 😞


Have you tried making a call to Application.DoEvents* in between the first Refresh and the Sleep?

*warning - many people have strong feelings about using DoEvents

View solution in original post

0 Kudos
7 Replies
AlexanderGray
Honored Contributor
I think you might only be able to do this with a timer (which runs on a seperate thread, so beware of threading.)  I tried it and looks like the view waits for the processing (sleep) to be finished on the main thread before it redraws.
0 Kudos
DariusDarius
Emerging Contributor
I think you might only be able to do this with a timer (which runs on a seperate thread, so beware of threading.)  I tried it and looks like the view waits for the processing (sleep) to be finished on the main thread before it redraws.


Thanks for replying. Well im not really sure what am I supposed to do. I mean I don't know how. By Separate thread you mean create a function? 😞
0 Kudos
JeffMatson
Frequent Contributor
Thanks for replying. Well im not really sure what am I supposed to do. I mean I don't know how. By Separate thread you mean create a function? 😞


Have you tried making a call to Application.DoEvents* in between the first Refresh and the Sleep?

*warning - many people have strong feelings about using DoEvents
0 Kudos
DuncanHornby
MVP Notable Contributor
Have you tried making a call to Application.DoEvents* in between the first Refresh and the Sleep?

*warning - many people have strong feelings about using DoEvents


I use Application.DoEvents all the time in my VB .net applications; love it, usually resolves all those sorts of screen refresh problems and I've never observed any significant slowing down of the application. This is how I would call it in VB.

Dim App As New Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase
App.DoEvents()
0 Kudos
deleted-user-VeC5jUIlNXtq
Deactivated User
Have you tried making a call to Application.DoEvents* in between the first Refresh and the Sleep?

*warning - many people have strong feelings about using DoEvents


Sorry to jump in...but could you explain what you mean by people have strong feelings?

I just wrote myself a note to test .DoEvents in some of the applications I develop/maintain, but you got me curious.

Thanks 🙂
0 Kudos
DariusDarius
Emerging Contributor
Have you tried making a call to Application.DoEvents* in between the first Refresh and the Sleep?

*warning - many people have strong feelings about using DoEvents


Well i tried and to my surprise ... it worked! 🙂 Thank you very much!
0 Kudos
JeffMatson
Frequent Contributor
Well i tried and to my surprise ... it worked! 🙂 Thank you very much!


No problem, glad you got it working.


Duncan and Geoff, here is one thread that illustrates why I added a note of caution:
http://stackoverflow.com/questions/5181777/use-of-application-doevents

Like you, I've been using it (sparingly) for years without noticing any negative side effects - I just didn't want the original poster to be caught unaware, in case "the boss" had a vendetta against DoEvents 🙂
0 Kudos