473,385 Members | 1,344 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,385 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 4654
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: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
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...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.