tags:

views:

44

answers:

2

I want to create a new instance of a form in a BackgroundWorker. I've noticed that when I do this, the newly launched form freezes.

Frozen form

Why does this form freeze? How can I get around this?

+3  A: 

It's freezing because you're creating the form in the wrong thread - there's no event loop running in the background thread.

You should only create or touch UI elements in the UI thread. BackgroundWorker provides some hooks for this, or you can use Control.Invoke/BeginInvoke.

Jon Skeet
A: 

When a form "runs" it needs to have a thread that runs the WndProc and handles incoming messages from Windows. What you should consider doing here is using the Application.Run() method to start up your form. This will do the necessary work to make sure the WndProc is running properly, and I belive you can call this from your Background thread if necessary.

Coding Gorilla
You're partly right but this cannot come to a good end. The thread that pumps a message loop must be STA to make basic stuff like cut and paste and drag+drop work. The BGW thread is a threadpool thread, it is always MTA and cannot be changed.
Hans Passant