3

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.

Community
  • 1
  • 1
atoMerz
  • 7,534
  • 16
  • 61
  • 101
  • What is the context? And why have you only accepted half of answers provided to you? – Jonathan Wood May 04 '11 at 17:06
  • I don't know why it should be considered a hack/abuse in general but in the question you referenced it's not a good solution. The major problem in the other post's code was to Sleep() on the UI thread, which is something you should generally avoid. – Ben Schwehn May 04 '11 at 17:07
  • @Jonathan Wood, because I didn't get the answer I was looking for? – atoMerz May 04 '11 at 17:09
  • @Ben Schwehn, There's a comment in that post, that says calling refresh() and some other function if used improperly is hack/abuse, and then the comment on my answer(which is removed now) made me doubt about it. – atoMerz May 04 '11 at 17:11
  • @AtoMerZ: Well, it might be because the question wasn't well formed. As I've already pointed out (and you've ignored), you haven't given any context for where you would use this. Either way, it's your choice. But I would expect less answers in the future if you don't accept any answers from people just trying to help. – Jonathan Wood May 04 '11 at 17:14

4 Answers4

3

The MSDN has the answer.

Control.Refresh:

Forces the control to invalidate its client area and immediately redraw itself and any child controls.

Control.Invalidate:

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.

Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214
  • I don't quite get it, I mean it to redraw itself immediately, so I can see result, or else I'd have waited for the form to update itself, And does the bold text mean I should/can use Invalidate instead? – atoMerz May 04 '11 at 17:15
  • 1
    @AtoMerZ Indeed, you can *and should* use `Invalidate` instead. The update will still happen immediately (as far as the user can see) but it’s friendlier to Windows since it doesn’t interrupt the normal message flow. – Konrad Rudolph May 04 '11 at 18:40
2

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.

Guffa
  • 687,336
  • 108
  • 737
  • 1,005
0

by simple words Refresh() will reloads the UI, when some change

anishMarokey
  • 11,279
  • 2
  • 34
  • 47
0

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.

adrianm
  • 14,468
  • 5
  • 55
  • 102