How to invoke a form to show?

A

Altramagnus

I have a form first created my the main thread.
I have another thread monitoring the IO.
Upon receiving certain message, I want to show/unhide the form.

When the form is first created ( handle is not created until it is first
shown ).
How do my IO thread invoke the form to show? ( in order to invoke the form,
the form handle must be created )

Thanks.
 
A

Armin Zingler

Altramagnus said:
I have a form first created my the main thread.
I have another thread monitoring the IO.
Upon receiving certain message, I want to show/unhide the form.

When the form is first created ( handle is not created until it is
first shown ).
How do my IO thread invoke the form to show? ( in order to invoke the
form, the form handle must be created )

Thanks.

2 suggestions: You could call CreateHandle in the main thread after the form
has been created. Or, use an empty dummy form created in the main thread
used by (Begin)Invoke in the 2nd thread. The target procedure in the main
thread then shows the actual form.

Armin
 
A

Altramagnus

Thanks

The first method would not work because,
even you explictly call CreateHandle, the handle would still not be created
until the form is first visible.
In other words, if the form is not visible, createHandle does nothing.

The second method works if i have a existing form that is already shown.
It will still be a problem if I have no existing form.

Thanks.
 
A

Armin Zingler

Altramagnus said:
The first method would not work because,
even you explictly call CreateHandle, the handle would still not be
created until the form is first visible.
In other words, if the form is not visible, createHandle does
nothing.

Sure? I tried it and it worked. In a new project, add a button and this code
(sorry, VB.NET, but you'll see the point):

Shared f As Form1
Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click

f = New Form1
f.CreateHandle()
Dim t As New Threading.Thread(AddressOf a)
t.Start()
End Sub
Private Delegate Sub d()
Private Shared Sub a()
f.BeginInvoke(New d(AddressOf b))
End Sub
Private Shared Sub b()

End Sub

Exception without createhandle, no exception with createhandle.

The second method works if i have a existing form that is already
shown. It will still be a problem if I have no existing form.

Yes, that's why you could add one, but you don't have to show it.


Armin
 

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