"Refresh" method does not refresh

D

Dom

A program I have contains a Listview that keeps track of different
stages in the program. For example, a line in the listview may
indicate "starting to read the database", or "producing averages".
The method used to write these lines ends with:

lstview1.Refresh();
this.Refresh();

The second line seems like overkill to me, but I added it because the
first line did not work alone. In fact, the two lines together don't
work either. This confuses the users, who think the program is stuck.

How do I refresh things?

Dom
 
J

Jeff Johnson

A program I have contains a Listview that keeps track of different
stages in the program. For example, a line in the listview may
indicate "starting to read the database", or "producing averages".
The method used to write these lines ends with:

lstview1.Refresh();
this.Refresh();

The second line seems like overkill to me, but I added it because the
first line did not work alone. In fact, the two lines together don't
work either. This confuses the users, who think the program is stuck.

How do I refresh things?

Is the code that's processing the data running on the main thread? If so,
and it's a long-running process, that's probably the issue. Put that code in
a separate thread. You'll have to do a little bit of extra coding to update
the UI, since updates can only be performed by code on the main UI thread,
but there are a zillion examples of doing this on the 'Net. Just search for
".net update main ui from other thread invoke" and you should get more hits
than you can eat.
 
R

Registered User

A program I have contains a Listview that keeps track of different
stages in the program. For example, a line in the listview may
indicate "starting to read the database", or "producing averages".
The method used to write these lines ends with:

lstview1.Refresh();
this.Refresh();

The second line seems like overkill to me, but I added it because the
first line did not work alone. In fact, the two lines together don't
work either. This confuses the users, who think the program is stuck.

How do I refresh things?

One possible solution would be to use data binding with an implementation of
INotifyPropertyChanged.
<http://msdn.microsoft.com/en-us/library/ms229614.aspx>

regards
A.G.
 
D

Dom

Well,what I do is this ...

Open a DB connection
Process an SQL statement
Add item to list view and refresh

Process an SQL statement
Add item to list view and refresh

Etc.

Maybe what I should is ...

Open a DB connection
Process SQL statement
Close DB connection
Add item to list view and refresh

Open a DB connection, etc.

Would that change things?

Dom
 
D

Dom

Well,what I do is this ...

Open a DB connection
Process an SQL statement
Add item to list view and refresh

Process an SQL statement
Add item to list view and refresh

Etc.

Maybe what I should is ...

Open a DB connection
Process SQL statement
Close DB connection
Add item to list view and refresh

Open a DB connection, etc.

Would that change things?

Dom

I got the answer. Instead of the "Refresh()" method, use
Application.DoEvents(). I suppose there is some danger here of firing
off unintended mouse clicks, but that's the price you pay.

Dom
 
J

Jeff Johnson

I got the answer. Instead of the "Refresh()" method, use
Application.DoEvents(). I suppose there is some danger here of firing
off unintended mouse clicks, but that's the price you pay.

I'm surprised no one has chimed in with a rebuke on how bad a choice it is
to use DoEvents. I'd recommend you read up on it; it seems to be discouraged
in many circles.
 
A

Arne Vajhøj

I'm surprised no one has chimed in with a rebuke on how bad a choice it is
to use DoEvents. I'd recommend you read up on it; it seems to be discouraged
in many circles.

True.

It is in most cases a poor workaround for a bad design.

Much better to fix the design.

Those interested in the juicy details can Google:
application doevents evil

Arne
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top