Like in this example:
someImage.Source = newSource;
someImage.refresh();
A few days ago in this post I responded with refresh() and I got feedback that it's a hack/abuse. I don't understand why.
Like in this example:
someImage.Source = newSource;
someImage.refresh();
A few days ago in this post I responded with refresh() and I got feedback that it's a hack/abuse. I don't understand why.
The MSDN has the answer.
Forces the control to invalidate its client area and immediately redraw itself and any child controls.
Invalidates the entire surface of the control and causes the control to be redrawn. […] Calling the Invalidate method does not force a synchronous paint
[Emphasis mine]
The point is that Refresh
, unlike Invalidate
, forces a synchronous call, which effectively interrupts the default event flow in forms and cuts the line in the message queue. This may cause other window messages (events from the operating system) to be delayed.
The Refresh
method call is not needed at all if you have a responsive user interface. Setting the Source
property creates a message that invalidates the display of the control, so it will be refreshed automatically when that message is handled.
It's only if your code contains a long running loop, so that it doesn't handle messages at all for a long period, that you need to use the Refresh
method. Such a long running loop should be avoided, as it causes the user interface to be unresponsive.
Because most gui frameworks handle refreshes/updates automatically if you use them correctly.
With refresh you work around the symptom (something is not automatically updated) instead of solving the root cause.
The problem is that Refresh usually start to spread like a virus. You insert it in one place and suddenly you need it in a second place, and third etc.