473,325 Members | 2,771 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,325 software developers and data experts.

How to call Control.Invoke for a property procedure?

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

Jul 21 '05 #1
4 4649
Ben Reese <Be******@discussions.microsoft.com> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Jul 21 '05 #2
"Ben Reese" <Be******@discussions.microsoft.com> wrote>
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?
Jul 21 '05 #3
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.Thr ead.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

"Patrick" wrote:
"Ben Reese" <Be******@discussions.microsoft.com> wrote>
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?

Jul 21 '05 #4
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)).

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


Jul 21 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: rawCoder | last post by:
Hi I am having this InvalidOperationException with message Cannot call Invoke or InvokeAsync on a control until the window handle has been created This is raised when i try to invoke a method...
0
by: Paul Tomlinson | last post by:
Morning all. Let me describe my problem. Main thread creates child objects and threads and starts them Timer on the main thread (which only gets activated after the child threads have started)...
2
by: Paul Bromley | last post by:
Hi, Can someone point me in the right direcion here. I have been a hobbyist programmer for some time with VB6 and am moving to Vb.Net. I am afraid that in the past I did not uderstand too well...
0
by: Pawan Narula via DotNetMonster.com | last post by:
hi all, i'm using VB.NET and trying to code for contact management in a tree. all my contacts r saved in a text file and my C dll reads them one by one and sends to VB callback in a sync mode...
4
by: Ben Reese | last post by:
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...
13
by: PW | last post by:
Hi, This is bizarre. I've check and rechecked the spelling, cut and paste the name of the form (when trying to rename the form) and the control yet I still get a parameter prompt when the form...
6
by: HolyShea | last post by:
All, Not sure if this is possible or not - I've created a class which performs an asynchronous operation and provides notification when the operation is complete. I'd like the notification to be...
1
by: euroluxantiques | last post by:
Hi All, Using VB.Net with SQL Server. I have a form that I create a number of Picture Boxes on dynamically, based on a stored procedure that retrieves a listing of images available for a...
1
by: =?Utf-8?B?RGF2ZQ==?= | last post by:
I have a class that Form1 calls to do some work. I would like to report back progress from the class to a richtextbox, will call it m_report, on the form. Like in the class, Form1.m_report.Text=...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.