Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Creating and displaying a new form

Status
Not open for further replies.

Sen7inel

IS-IT--Management
Feb 14, 2001
90
0
0
FI
My app has a main form. The form has a button which hides the main form and then tries to create a HttpWebRequest to get some info. While the (synchronous) webrequest resolves and connects, I'd like to display a smaller non-modal form with no controls except for a label that states that something is actually happening.

Now, after hiding the main form I instantiate a new form and make it look right with the label and no controlboxes etc. I then call theForm.Show(); .. And duh, the form doesn't show up right. Windows draws a new empty window frame, size being what I wanted to, but it seems broken and has nothing in it. Once the webrequest stuff is processed, my code disposes of the little form and continues as ever.

theForm.ShowDialog(); seems to be blocking, so I wouldn't want to use that. I can of course create a new thread, but I guess I'm just doing something wrong here.

I also managed to show the form by creating a new thread for it, and displaying the form by calling Application.Run(theForm); ..

So what gives, why doesn't the simple .Show() work? Does every form need a thread of its own or something like that?
 
My guess is that it's doing just what you want, except it's not getting painted completely to the screen before it gets interrupted by the HttpWebRequest. Try making the main form sleep for a couple seconds right after

LittleForm.Show()

and see if that takes care of it.

Ben

"If thine enemy offend thee, give his child a drum." - Anonymous
 
This doesn't seem to help - the form stays broken. Is it so that every non-blocking (i.e. non-dialog?) form needs this "message loop"? And speaking of which, what is this "message loop"? I haven't got all that much experience in Windows programming and .NET documents don't tell me much about these, although they are mentioned here and there (Application.Run() for example).
 
I'm really not sure, Sen7inel... never seen that kind of error before. Maybe someone else will chime in and solve this one for us. The only thing I can come up with as a workaround is to make the second form perform the request and return the results to the original form; that way you could use a ShowDialog().

Ben

"If thine enemy offend thee, give his child a drum." - Anonymous
 
You were pretty close to the answer when you suspected it had something to do with the message loop. The message loop is run from within the
Application.Run(theForm);
method. Every time your application responds to anything (mouse click, paint request, etc.) the method is getting called from the message loop.

A problem occurs when a handler for a message dispatched from the loop doesn't return quickly enough. In your case, the user selects the "go" button, and then you put up a form and try to get the information from the website. You haven't returned yet from the "go" handler, so everything else (including the paint request on your second form) is bottled up waiting for the "go" handler to return.

You could handle this with a second thread for the form, but there is a more direct approach. The problem really starts with the blocking call via HttpWebRequest. Why not put that processing in a seperate thread? Then you can return quickly from your "go" button, and leave your UI single threaded.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top