Click here to Skip to main content
15,843,727 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a thread which has a function to Generate and Show a new Form. The thread do an invinite loop using while true. Because of that, my form always show up only white form. I must stop the invinite loop to make the form work.

The problem is, i don't want to stop the invinite loop. Can i ask the form that created in my thread to move to the other thread??

Thanks for the help.. :)

Your form should be created on the UI thread, not your background thread. I'd marshal a call to a method that creates the form back to the UI thread. This will keep your infinite loop going as well as eliminate the need for Sleep and DoEvents.
Share this answer
Jimmanuel 15-Jul-10 13:55pm    
Reason for my vote of 5
Good answer.
Dwi Prawira 15-Jul-10 20:11pm    
i think my form must generate by thread.. because, my application is messenger. the client have a thread that always listen to server.. whenever server send a request to the client, the client should show the form.. Application.DoEvents works great on my application.
Dave Kreskowiak 15-Jul-10 22:04pm    
Wrong. Your form does NOT have to be generated by a thread. Your CONTENT for the form can come from a thread. You then just invoke a method on the UI thread to update the form with the new content.

Creating and using forms and controls introduces bugs in your code that are nearly impossible to replicate and find because of bad cross-thread operations. The general rule of thumb is if the user can see the control, it has to be created and updated on the UI (startup) thread. If you want to have problems that are a @#*%# to find and fix, keep doing it your way. It MAY work for you on your machine just fine, but out in the wild, on any number of users PC, I guarantee that your existing method is not going to hold up.
Wow... Thats work great.. thanksss...
Share this answer
Johnny J. 14-Jul-10 3:20am    
Sure it works. If you want to know WHY it works, check out my reply to this guy who had a similar problem:
Dwi Prawira 14-Jul-10 5:22am    
OK... Thanks for your advise.. :)
Insert an Application.DoEvents or a Thread.Sleep(...) in your infinite loop
Share this answer
Nish Nishant 13-Jul-10 23:00pm    
Reason for my vote of 5
Worth a 5.
Dave Kreskowiak 15-Jul-10 11:23am    
Reason for my vote of 1
Forms should not be created by the background thread at all and this is no solution to the problem.
Jimmanuel 15-Jul-10 13:53pm    
Reason for my vote of 1
Calling Application.DoEvents in this case is a misuse of that method.
Johnny J. 15-Jul-10 14:20pm    
Bullshit Jimmanuel - That's a statement that needs proving. Too bad I can't downvote a comment...
Jimmanuel 15-Jul-10 16:13pm    
Well then, how about telling us how you really feel :)

Dave's answer is IMO the correct one. Modeless forms should not be shown from background threads. Period. Whenever you attempt it you're forced to call Application.DoEvents because otherwise the form would never get a chance to process window messages and it'll appear frozen, as the OP has found out. Why I consider this a misuse is because background threads are for doing background work, not running windows message pumps. To show a form the form should have it's own thread exclusively for processing messages but since in this case the primary purpose of the thread is to do some background work, calling Application.DoEvents is improper.

Showing a modeless form from a background thread and then sleeping does nothing because if the thread is asleep then it obviously can't process the window messages and the form will stay as frozen as it was before (and i just tested this to confirm).

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900