Incrementing a label on a windows form

M

Mo

Hi,

I am reading a line off of a local file and using it to update records
on a database table. I am using a progress bar to show the progress
which is tied to the number of records inserted. I am trying to also
show in real time the count of the records which have been processed. I
havea label on the form and in the loop I have Label1.text =
i.ToString() where i is the loop counter.I am using a file watched to
trigger the process when the file is created in the directory. It might
be something to do with the asynchronousity of the process. Any ideas/

Thanks
 
A

Andy

If you're doing the work without using the BackgroundWorker component
(or a background thread, pre .Net 2) then your UI will not update while
you're running in the loop.

Use the BackGroundWorker, and have it raise ProgressChange events.

Andy
 
S

Stoitcho Goutsev \(100\)

Mo,

What is the problem actually? I think it is that the lable doesn't update
while inside the loop.

If this is the case I can see two possible reasons. I also assume that you
use FileSystemWather compoenent for monitoring the file

1. You haven't set file watcher's SyncronizingObject - in this case the
events are fired in a worker thread and trying the change the text in the
label is wrong. Only the UI thread created the lable should alter the text

2. You have set file watcher's property and your code is executed by the UI
thread - in this case
when the program loops the application doesn't pump messages from the
message queue thus paint events are not precessed. The easiest solution is
to call Application.PreocessEvents periodically from inside the loop.
 

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