all groups > dotnet clr > december 2005 >
You're in the

dotnet clr

group:

Threading in .NET



Threading in .NET sonali_reddy123 NO[at]SPAM yahoo.com
12/28/2005 5:46:31 AM
dotnet clr: 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
Re: Threading in .NET Stefan Simek
12/29/2005 12:07:23 PM
[quoted text, click to view]

You should call Control.Invoke from the background thread to call a
method in the UI thread that will show the modal dialog. When the method
completes, the control will be returned to both the UI and the
background thread.

HTH,
Re: Threading in .NET Lucvdv
1/1/2006 1:28:16 PM
[quoted text, click to view]

Maybe I misunderstood what you're trying to do, but it seems you don't
need/want to use a separate thread at all.

When you start another process (another application), it starts in a
thread of its own automatically.

If you want your code to wait until it completes, you tell so when you
start the process.

In VB you can use the Shell function:

Shell("notepad.exe", AppWinStyle.NormalFocus, True)

"True" as the third parameter means "wait until it finishes".

Another solution that does the same:

Dim p As New System.Diagnostics.Process
p.StartInfo.FileName = "notepad.exe"
p.Start()
p.WaitForExit()

Both forms "freeze" your application until the process exits though.
Trying to avoid that by using a separate thread just pushes the
problem toward the horizon: you can create another thread and make
that wait until the process exits, but then you have to make your app
wait for that thread, and the easiest way to do that is to make your
app freeze ;)



Also, if you use a separate thread, always make sure that all objects
(forms, buttons, ...) are accessed only from within the thread that
created them. The CLR provides functionality to accomplish this.

I find a small function added to a form's code the easiest. VB sample
to change a TextBox's text:

Private Delegate Sub dSetText(ByVal NewText As String)
Public Sub SetText(ByVal NewText As String)
If Me.InvokeRequired Then
Me.Invoke(New dSetText (AddressOf SetText), _
New Object() {NewText})
Else
Me.TextBox1.Text = NewText
End If
End Sub

You can call SetText from within any thread, it will always set the
text in the context of the right thread, so you don't have to keep
track of where it's being called from.


Note that InvokeRequired can be called on all forms-related objects,
but it isn't shown by intellisense (you could also use
Re: Threading in .NET Yogesh
1/1/2006 7:20:24 PM
Hi Sonali

I think you are calling the ShowDialog() function from the secondary
thread that is why its only blocking that particular thread(Secondry
Thread) .For blocking the GUI please call the ShowDialog() from the
primary thread.
Re: Threading in .NET Ibrahim DURMUS
1/5/2006 1:22:39 AM
<<
[quoted text, click to view]

AddThis Social Bookmark Button