views:

163

answers:

2

Hi,

My application (C#, VS2008) loads information from a database (SQL Server 2008 Express) over the network. During (possibly) longish waits, I want to have a 'Loading...' dialog box appear running on a different thread, but only if the operation takes more than a specific time period (say 500ms).

So, I have so far got my loading form being displayed after 500ms (if the operation lasts that long) without setting the loading dialog form's owner (i.e calling LoadingDialog.ShowDialog()), but when I try to call LoadingDialog.ShowDialog(IWin32Window owner) with owner set to the main form (passed in through the thread's parameter) I get the InvalidOperationException about accessing controls across threads.

My basic question is: Can I create and ShowDialog a form with the owner parameter set to a form on another thread? I want to do this so that the loading dialog is modal the rest of the application, i.e. like any other dialog takes the focus and disallows input elsewhere.

Can anyone offer a suggestion? I have read heaps about splash screens with no luck, also about Invoke and BeginInvoke with no luck. Is there a different way I should go about this?

Please feel free to ask for clarification if you don't understand.

Merci (as they say in French),

Jacob.

+1  A: 

Hello,

All the UI components run on single sole thread which runs over the win32 message loop. You can not run any UI component in another thread. This architecture persists since Windows 3.1.

Vitaliy Liptchinsky
In Win32, you _can_ run UI on multiple threads, as long as each thread runs its own message loop. There's at least one sample in the Win32 SDK that demonstrates this. I don't know whether this is possible in WinForms, and I've never had cause to do it in Win32 or WinForms, myself.
Roger Lipscombe