How to call Control.Invoke for a property procedure?

G

Guest

Simplified.
I have 2 forms running on seperate threads (and message queues)
I am having some odd problems with with the form on the second
Thread/message queue freezing and/or dyinig, seemingly at random.

I have been carfull always to call Form2.Invoke(someDelegate) Whenever I
want to access a method on the second form from the first.
I have tracked down 1 remaining occurence Where from 1 wants to disable a
button on form 2

Public Class Form 1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Form2.ButtonClose.Enabled = Flase
End Class

I think that this may be the cause of my problems. I am having trouble
however defining a delegate to pass to Form2.ButtonClose.Invoke

Public Class Form 1
Private Delegate Function EnableCloseGet() As Boolean
Private Delegate Sub EnableCloseSet(ByVal Value as Boolean)
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
' I have tried both of these
'1
Dim _EnableCloseGetDelegate As New EnableClose(AddressOf
Me.ButtonClose.Enabled)
Form2.ButtonClose.Invoke(_EnableCloseGetDelegate)

'2
Dim _EnableCloseSetDelegate As New EnableCloseSet(AddressOf
Form2.ButtonClose.Enabled)
Dim _args() As Object = {False}
Form2.ButtonClose.Invoke(_EnableCloseSetDelegate, _args)
End Class

Both approaches give me compile errors respectively
"Method 'Public Property Enabled() As Boolean' does not have the same
signature as delegate 'Delegate Function EnableCloseGet() As Boolean'."
"Method 'Public Property Enabled() As Boolean' does not have the same
signature as delegate 'Delegate Sub EnableCloseSet(Value As Boolean)'."

Where am I going wrong please. is there a special type of delegate that I
need to use or sometinig?

Ben
 
J

Jon Skeet [C# MVP]

Ben Reese said:
Simplified.
I have 2 forms running on seperate threads (and message queues)
I am having some odd problems with with the form on the second
Thread/message queue freezing and/or dyinig, seemingly at random.

I have been carfull always to call Form2.Invoke(someDelegate) Whenever I
want to access a method on the second form from the first.
I have tracked down 1 remaining occurence Where from 1 wants to disable a
button on form 2

Public Class Form 1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Form2.ButtonClose.Enabled = Flase
End Class

I think that this may be the cause of my problems. I am having trouble
however defining a delegate to pass to Form2.ButtonClose.Invoke

<snip>

Two ways of doing this:
1) Use reflection to find the MethodInfo for the setter, and create a
delegate for that, using Delegate.CreateDelegate.

2) Write a method which takes a boolean and just sets the property -
then use a delegate to that method.
 
P

Patrick

Ben Reese said:
I have 2 forms running on seperate threads (and message queues)

How is this possible? I was under the impression that all Windows UI work
has to be done on the UI thread...

What am I missing?
 
G

Guest

Patrick

You can run a new form on a different thread like this:

Public Class Form1
Inherits System.Windows.Forms.Form

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim MyThread As New System.Threading.Thread(AddressOf RunForm2)
MyThread.Start()

End Sub

Private Sub RunForm2()
Dim Form2 As New Form1

Windows.Forms.Application.Run(Form2)

End Sub


Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button2.Click

Windows.Forms.MessageBox.Show(System.Threading.Thread.CurrentThread.GetHashCode)
End Sub


However if you forms need to comunicate with each other you must be carefull
because you need always to update properties of forms (and controls) an the
same thread that created them. This is the source of my problem.
You must also be very carefull if other objects (that are not forms or
controls)running on different threads raise events that get back to the
control as the event will be on the objects thread not the controls.
HTH
 
G

Guest

Also

The second form is running on its own message queue as well as its own thread.
This prevents it from being locked by any modal forms that are shown from 1
,or in my case a form in a VB6 dll form shown by form one (which can only be
shown modally from VB.Net ( I have no idea why)).

 

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