> Hi all,
>
[quoted text clipped - 18 lines]
>
> Thanks in advance
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,
Stefan
Ibrahim DURMUS - 04 Jan 2006 23:22 GMT
<<
>> Hi all,
>>
[quoted text clipped - 26 lines]
> HTH,
> Stefan
> Hi all,
>
[quoted text clipped - 3 lines]
> 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
Textbox1.InvokeRequired and TextBox1.Invoke here, instead of Me.*).
Yogesh - 02 Jan 2006 03:20 GMT
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.