Threading in .NET

S

sonali_reddy123

Hi all,

I have a problem regarding the threading issues in .NET. Actually I
have a application in which i execute a process by invoking the thread
in the background and at the same time I
want to display a modal dialog which will only block my UI. It doesn't
have to do anything with my thread executing in the background.

Actually the dialog is getting displayed as result of the process
carried out by a thread in background. So the thread accepts the output
from the dialog and continues with furter
processing.

The problem is the dialog which I am displaying is not getting
displayed as a modal dialog.
And I don't want the user to interact with the background UI while the
dialog is getting displayed.

Is it that Calling ShowDialog from the thread event handler causing the
problem if so what is the solution.

Thanks in advance
 
J

Jon Shemitz

Actually the dialog is getting displayed as result of the process
carried out by a thread in background. So the thread accepts the output
from the dialog and continues with furter
processing.

The problem is the dialog which I am displaying is not getting
displayed as a modal dialog.
And I don't want the user to interact with the background UI while the
dialog is getting displayed.

Is it that Calling ShowDialog from the thread event handler causing the
problem if so what is the solution.

Hard to tell from your description what the problem is. General rule
is: You should only ever touch controls from the thread that creates
them. If you create the modal dialog in the main thread, you should
ShowDialog it from the main thread, using something like
MainForm.Invoke().
 
T

Tim_Mac

hi Sonali,
from your background thread, you should call a method using BeginInvoke to
transfer execution of that method to the main UI thread. then you call
ShowDialog from that method and it will block the UI thread only, and the
modal function will work normally.
example below (untested):

private void MyAsyncMethod()
{
// do stuff.. running on background thread
// ...

// ok, now want to ShowDialog on the main UI thread
BeginInvoke(new MyDelegate(this.MethodForShowDialog), null, null);

// the background thread will continue running asynchronously,
regardless of ShowDialog() on the main UI thread
}

delegate void MyDelegate();
private void MethodForShowDialog()
{
// now running on UI thread
MessageBox.ShowDialog(...whatever...);
}

is this what you want?
tim
 

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

Top