By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,960 Members | 990 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,960 IT Pros & Developers. It's quick & easy.

Threaded UI

P: n/a
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
Mar 16 '06 #1
Share this Question
Share on Google+
4 Replies


P: n/a
On Thu, 16 Mar 2006 08:38:39 -0800, Todd wrote:
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).
Mar 16 '06 #2

P: n/a

Mehdi wrote:
On Thu, 16 Mar 2006 08:38:39 -0800, Todd wrote:
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).


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

Mar 16 '06 #3

P: n/a
Any way to tell the dialog created in the background thread to use the
foreground app's thread???

"Brian Gideon" wrote:

Mehdi wrote:
On Thu, 16 Mar 2006 08:38:39 -0800, Todd wrote:
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).


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

Mar 16 '06 #4

P: n/a
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.
}
}

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

"Brian Gideon" wrote:


Mar 16 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.