Threaded UI

G

Guest

I've got a situation where I need to display a modal dialog from within a
background processing thread. The problem I'm having is that it needs to
display as a modal window to the thread's parent GUI app. When I try this I
get a pseudo-modal dialog (more like a top-level window)... the dialog is
always on top of the parent app's window, but the user can still access the
parent window and its controls.

Is there any way to accomplish true modal dialog functionality when opening
a dialog from within a separate thread?

Thanks,
Todd
 
M

Mehdi

I've got a situation where I need to display a modal dialog from within a
background processing thread. The problem I'm having is that it needs to
display as a modal window to the thread's parent GUI app. When I try this I
get a pseudo-modal dialog (more like a top-level window)... the dialog is
always on top of the parent app's window, but the user can still access the
parent window and its controls.

Is there any way to accomplish true modal dialog functionality when opening
a dialog from within a separate thread?

Place the code showing the modal dialog in a function then marshall the
call of this function to the UI thread using the Invoke method of any
control that has been created in the UI thread. The result will be just
like if you had shown the modal dialog from the background thread and the
dialog will behave properly. You should also create the dialog object
itself in the UI thread (one solution among others to do that would be to
create it when you start your application before starting the background
thread, store it as a member variable and then show this instance of the
dialog from your background thread).
 
B

Brian Gideon

Mehdi said:
Place the code showing the modal dialog in a function then marshall the
call of this function to the UI thread using the Invoke method of any
control that has been created in the UI thread. The result will be just
like if you had shown the modal dialog from the background thread and the
dialog will behave properly. You should also create the dialog object
itself in the UI thread (one solution among others to do that would be to
create it when you start your application before starting the background
thread, store it as a member variable and then show this instance of the
dialog from your background thread).

I'd go with the first option. The Show method, like most methods on a
form or control, has thread affinity requirements. In other words, it
must be called from the UI thread. The ISynchronizeInvoke methods
(Invoke, BeginInvoke, etc.) are exceptions.

Brian
 
G

Guest

Any way to tell the dialog created in the background thread to use the
foreground app's thread???
 
B

Brian Gideon

No. You must create the form on the UI thread. Likewise, you can only
access it from the UI thread. That is mandatory.

The following code snippet should help explain how this might be done.
I've omitted some stuff brevity, but I think you'll get the idea.

// .NET 1.1
public class YourBackgroundWorkerClass
{
// Get a reference to one of your app's forms here.
private Form mainForm;

private void YourBackgroundThreadMethod()
{
// Do some stuff.

DisplayYourDialogForm();

// Do some more stuff.
}

private void DisplayYourDialogForm()
{
if (mainForm.InvokeRequired)
{
Delegate method = new ThreadStart(this.DisplayYourDialogForm);
mainForm.Invoke(method, null);
}
else
{
YourDialogForm form = new YourDialogForm();
form.ShowDialog();
}
}
}

// .NET 2.0
public class YourBackgroundWorkerClass
{
// Get a reference to one of your app's forms here.
private Form mainForm;

private void YourBackgroundThreadMethod()
{
// Do some stuff.

ThreadStart method = delegate()
{
YourDialogForm form = new YourDialogForm();
form.ShowDialog();
};

mainForm.Invoke(method, null);

// Do some more stuff.
}
}
 

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

Similar Threads

modal popup window help 5
Posting from a modal dialog 4
Regarding usercontrol in .NET 1
post back event for a button 8
ShowDialog problem 2
Modal Dialog 1
Question about thread safety... 6
Threading in .NET 2

Top