[quoted text, click to view] sonali_reddy123@yahoo.com wrote:
> 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.
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