Show a from immediately

  • Thread starter Thread starter Dom
  • Start date Start date
D

Dom

On my main form, I click a button to bring up a second form. When the
second form comes up, I want to do the following:

1. change this.Cursor to Cursors.Wait
2. Execute the EnumerateServers (or whatever) method.
3. Add all Server names to a list box.
4. Change this.Cursor back again.

The reason for steps 1 and 4 is that step 2 can take so long. Of
course, all of this makes sense only if the user can actually see the
form. I've placed all the code in the Load event, but the user just
sees the border of the form for several seconds, making him think
something is going wrong. Same thing with the Activate event. Where
should I place it?
 
Dom,

What you should do on the Load event is to kick off a thread (using the
Thread class, or through the ThreadPool) which will perform the enumeration
for you. Then, when the task is done, you can call the Invoke method on the
main form to indicate that the operation is complete.
 
Dom said:
On my main form, I click a button to bring up a second form. When the
second form comes up, I want to do the following:

1. change this.Cursor to Cursors.Wait
2. Execute the EnumerateServers (or whatever) method.
3. Add all Server names to a list box.
4. Change this.Cursor back again.
You have to split that tasks.
In the Load event you should execute step 1 and then start a Thread that
executes Step 2. This Thread must somehow provide the result (the names of
the server + whatever additional information needed). This Thread may not
access the UI.
At the end, the thread should use the Invoke-method of the Form to invoke
the execution of step 3 and step 4. This will push the execution of this
steps to the GUI.

Additional the thread can use Invoke to show some intermediate result, like
filling the list box as the results are recieved or showing process.

So far
Christof
 
If you don't want or need to start threading, you should be able to simply
do this in the Form Load event:
1. change this.Cursor to Cursors.Wait
Me.Show()
Application.DoEvents()

2. Execute the EnumerateServers (or whatever) method.
3. Add all Server names to a list box.
4. Change this.Cursor back again.

/Johnny J.
 
Johnny Jörgensen said:
If you don't want or need to start threading, you should be able to simply
do this in the Form Load event:
<snip>

That wouldn't help much. While step 2, the GUI will still be blocked,
including PaintEvents. So after putting some other apps form in front of it,
it wouldn't repaint itself until after step 2 finished.

Christof
 
If you don't want or need to start threading, you should be able to simply
do this in the Form Load event:


Me.Show()
Application.DoEvents()

I'm in CSharp, not VB. There is no DoEvents, right?
 
Sadly, there is. As per the example, it is a static method on
Application; this is what VB uses under the bonnet, so there is no
real difference. I'm not sure I'd recommend it in a production app,
though ;-p

Marc
 
What you should do on the Load event is to kick off a thread (using the
Thread class, or through the ThreadPool) which will perform the enumeration
for you. Then, when the task is done, you can call the Invoke method on the
main form to indicate that the operation is complete.

A background worker component would make this even easier.
 

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

Back
Top