How repaint a from when a main thread is blocked ?

  • Thread starter Thread starter Boniek
  • Start date Start date
B

Boniek

Hi

My main thread is waiting for data from database ( opened Sqlconnection etc)
and my Main Form is also blocked. I have a ProgressBar on Status Bar which
show to a user how percent data is loaded. I use ThreadTimer and Thread but
I can't do anything because when a thread (or ThreadTimer) go to the line
ProgressBar.PefromStep() then it stop on that line. I think it's work like
that because the Main Thread (and main Form) is sleeping.
Can you give me any ideas how I can do that ? Maybe this problem disapear
when I load data in other thread and my Main Thread is free but it doesn't a
good idea.
Thank's all for any help.

Thank's Boniek
 
Maybe this problem disapear
when I load data in other thread and my Main Thread is free but it doesn't a
good idea.

It's the only possible solution, though. The main GUI thread paints
all your windows; if that thread is blocked you don't get window
updates, and that is that. You must place the blocking operation in a
separate worker thread. Be sure to check the various MSDN articles on
multithreading with Windows Forms for how to do this.
 
Hi,

I really don't think you have an option. If you want your main thread
to be free, well... it has to be free! If it's blocked by a call to
fetch data from the database, then it simply isn't going to do
anything until that call returns. The timer calls are returning fine
because they run on worker threads. But as long as the form thread is
blocked, you won't see the updates to the progress bar simply because
the form can't paint itself.

Ideally, you should offload such calls to another thread and keep your
UI thread unblocked at all times. Just remember that if you do choose
to use another thread, then you must use Form.Invoke to update the
progress bar.
 
A better strategy is to create a new thread, connect to the database on that
thread, and then have your main thread use
Thread.Join calls to wait and increment your progress bar during the
connection
 
Back
Top