UI update and threading

07-20-2006, 05:05 PM
To start off, I am using VS.NET 2005 and a little confused about (simple) threading.

Anyways, I have an application that will compare a source database to multiple "compare to" databases to ensure the structures are the same and I am have a few questions about updating the UI while a long process is running.

I want a progress bar and a status bar label to change continuously throughtout the comparison process. I know there are some issues with updating the UI on the same thread as my long running process, so I wanted to put the comparison process in it's own thread using the BackgroundProcess method..

1. The progress bar should be fairly simple, since it style is set to marquee, so I just want the UI to update.

I think moving the comparison process to it's own thread will fix this issue, but I would like some opinions on this.

2. I need the status bar label to change everytime a new "compare to" database is selected, but I read that I shouldn't/can't update the UI from a process running in a seperate thread, not sure if it's true, so any opinions would also be helpful.

What is the best way to update the status bar label text from the comparison process thread?

Any help and thoughts on this would be helpful.


07-22-2006, 12:31 PM
try using delegates for the comparision thread and just have that thread set the appropriate property of the statusbar or whatever

07-23-2006, 06:22 PM
1) Basically, if a task interferes with UI message handling and Application.DoEvents isn't able to keep the UI updated fast enough then the task may be a good candidate for a worker thread.

2) Any changes to the UI should be marshalled to the UI thread. You need to use delegates for this, but a delegate by itself is not enough since delegates are essentially function pointers and they're not thread-safe.

If you're using the BackgroundWorker component, the ProgressChanged and and RunWorkCompleted events occur on the UI thread (i.e. no extra work for you). If you're not, you'll need to use the Control.Invoke method to marshal method calls to the UI thread.

MSDN has a couple different examples on how to use the BackgroundWorker class.

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum