Unresponsive UI during lengthy operation ?

C

Cerebrus99

Hi all,

I'm making a Windows application that does some lengthy retrieval operations
from a database and possibly from a internet resource.

I want to show that the operation is going on, by using an Animated .gif in
a picture box. Also note that this PictureBox also acts as the button to
invoke this lengthy operation. In other words, the user will click the
PicBox to start the operation. When clicked, I update the Image property of
the PicBox to the Animated .gif, and then call the lengthy method. I also
change the Tooltip of the PicBox to something like "Retrieving... please
wait...". At the end of the operation, I want to reset the Image property of
the PicBox.

The problem is that the PicBox image just freezes until the operation is
complete, by then it is already time to reset the Image. How do I show the
progress in this case ? The tooltip however, gets changed, which means that
the problem is mainly with updating the image. I have also noticed that the
form will not repaint, if I bring another application over it, and restore
it.

I have tried to call Application.DoEvents() at the start of my lengthy
method, but it never seems to have any effect whatsoever.

My concepts are a bit shaky as far as Asynchronous calls are concerned, so I
was wondering if there is any other way to prevent the UI from freezing up.

Any help is appreciated.

Regards,

Cerebrus.
 
M

m.posseth

well there are two ways i would recomend


1. use multithreading ( start a seperate thread ,,,, Background worker
would be suitable for this )
2. start the operation asynchronous if it supports this feature (
webservices and database objects often provide a begin and end invoke )

maybe someone else has other options

regards

Michel Posseth [MCP]
 
G

Guest

Is Multi-threading the only answer

Yes, in my opinion. Run the lengthy operation on a new thread. Devise a
mechanism for communication between the new thread and the ui thread. There
are two easy choices.

1. Worker thread updates properties in a class or module with synclock, ui
thread has a timer and samples them regularly also using synclock.

2. Worker thread posts info to ui thread directly via invoke or
begininvoke/endinvoke.

Probably #2 is most commonly used, I prefer #1.
 
C

Cerebrus99

Thank you very much everyone for all that information !

I have now realized that knowledge of Multithreading and Asynchronous
operations were one area that I cannot do without. So, I have read up
on some of those useful links (Thanks for Carlos and Herfried).

I have now been able to implement the following in my application :

1. The Click of the PicBox now starts an asynchronous process to
retrieve all the data.
2. During this operation, the UI remains responsive and the PicBox
shows an Animating .gif.
3. I have learned not to violate the prime directive of Windows Thread
programming-"Though shalt not operate on a window from other than its
creating thread." Basically to use the Invoke method.

Some minor flaws remain, and I turn to you again for advice...

1. At the start of the operation, I changed the Cursor to
Cursors.WaitCursor, and I change it back to Cursors.Default after the
operation completes. But my Cursor changes almost immediately to the
Default cursor, even though the line instructing it to be set to
Default, is reached only later in the processing. Is this behaviour
changeable too ? I want to keep showing an Hourglass while processing
continues.

2. The retrieval operation seems to take longer than before. Is this
normal, or just a psychological reaction of mine ? :)

Thanks again, folks,

Regards,

Cerebrus.
 

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