473,396 Members | 1,756 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,396 software developers and data experts.

Best way to handle Control.InvokeRequired without duplicating code?


I'm trying to find a good way to handle Control.InvokeRequired without
duplicating four lines of code in every function/event. Typically
what I've seen in books is this:

If InvokeRequired Then
Invoke(new EventHandler(AddressOf thisFunc), new Object() { sender,
e})
Return
End If

At the start of every function. Is there a good way to extract these
four lines of code from the functions themselves? I figured I can
make an InvokeIfRequired function but I would still need three lines
in order to accomodate the Return.

The best I could come up with is to get at it from the sending side
and not the receiving side (code below) but that it feels like the
handler should be taking care of this and not the sender (ideally, the
framework would take care of it internally for events).

Also the downside to the sub I came up with is that you lose
compile-time checking on arguments.

other options?

Thanks,

Sam
Public Shared Sub SmartRaiseEvent( _
ByVal delegat As [Delegate], _
ByVal args As Object(), _
ByVal synchronous As Boolean)

For Each iDelegate As [Delegate] In _
delegat.GetInvocationList()

Dim target As Object = iDelegate.Target
If Not target Is Nothing AndAlso _
TypeOf target Is Control AndAlso _
DirectCast(target, Control).InvokeRequired Then

If synchronous Then
DirectCast(target, Control).Invoke(iDelegate, args)
Else
DirectCast(target, Control).BeginInvoke(iDelegate, args)
End If
Else
iDelegate.DynamicInvoke(args)
End If
Next
End Sub
Nov 21 '05 #1
2 4053
Hi

I think in common a class event is designed as three part.
1. the delegate function
2. the event of type with that delegate
3. a OnXXXX Function will which fired the event

So I think we can add the InvokeRequired code in the OnXXXX Function.

e.g.

Private Sub OnFilesFound(ByVal Sender As Object, _
ByVal e As System.EventArgs)
If Me.InvokeRequired Then
Dim del As New UpdateUIFilesFoundDelegate( _
AddressOf Me.UpdateUIFilesFound)
Me.BeginInvoke(del
Else
Me.UpdateUIFilesFound()
End If
End Sub

Asynchronous Execution in Visual Basic .NET
http://msdn.microsoft.com/library/de...us/dv_vstechar
t/html/vbasync.asp

Also the SmartRaiseEvent you provided is somewhat a common method will will
raiseevent on all kind of event, so the compile time check will be missing,
as we have to use "ByVal args As Object()" similar declaration to pass
parameters.

Or I think we may have another idea that we can add a OnSafeXXXX fuction
will marshal the invoke call.
e.g.
Private Sub OnFilesFound(ByVal Sender As Object, _
ByVal e As System.EventArgs)
RaiseEvent FilesFound(Sender,e)
End Sub

Private Sub OnSafeFilesFound(ByVal Sender As Object, _
ByVal e As System.EventArgs)
If Me.InvokeRequired Then
'Invoke the OnFilesFound here
'Since the Sender and EventArgs has passed the compiling time
check, we just has the raiseevent call running on the UI thread just as we
do in the single thread program.
End If
End Sub

If you still have any concern, please feel free to post here.

Best regards,

Perter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.

Nov 21 '05 #2
Hi

I think in common a class event is designed as three part.
1. the delegate function
2. the event of type with that delegate
3. a OnXXXX Function will which fired the event

So I think we can add the InvokeRequired code in the OnXXXX Function.

e.g.

Private Sub OnFilesFound(ByVal Sender As Object, _
ByVal e As System.EventArgs)
If Me.InvokeRequired Then
Dim del As New UpdateUIFilesFoundDelegate( _
AddressOf Me.UpdateUIFilesFound)
Me.BeginInvoke(del
Else
Me.UpdateUIFilesFound()
End If
End Sub

Asynchronous Execution in Visual Basic .NET
http://msdn.microsoft.com/library/de...us/dv_vstechar
t/html/vbasync.asp

Also the SmartRaiseEvent you provided is somewhat a common method will will
raiseevent on all kind of event, so the compile time check will be missing,
as we have to use "ByVal args As Object()" similar declaration to pass
parameters.

Or I think we may have another idea that we can add a OnSafeXXXX fuction
will marshal the invoke call.
e.g.
Private Sub OnFilesFound(ByVal Sender As Object, _
ByVal e As System.EventArgs)
RaiseEvent FilesFound(Sender,e)
End Sub

Private Sub OnSafeFilesFound(ByVal Sender As Object, _
ByVal e As System.EventArgs)
If Me.InvokeRequired Then
'Invoke the OnFilesFound here
'Since the Sender and EventArgs has passed the compiling time
check, we just has the raiseevent call running on the UI thread just as we
do in the single thread program.
End If
End Sub

If you still have any concern, please feel free to post here.

Best regards,

Perter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.

Nov 21 '05 #3

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

Similar topics

2
by: Ingo Schasiepen | last post by:
Hi there, i'm evaluating if c# is suited to replace a script language. Most of the elements of this language can be replaced with a c#-library, but it also has some realtime-like elements. E.g.,...
11
by: BoloBaby | last post by:
OK, check this out... I have a form with a panel control and button on it (outside the panel control). I have two event handlers - one handles the click event of the button on the form. The...
0
by: Samuel R. Neff | last post by:
I'm trying to find a good way to handle Control.InvokeRequired without duplicating four lines of code in every function/event. Typically what I've seen in books is this: If InvokeRequired Then...
3
by: JamesB | last post by:
Hello I have a form which contains a Listview control that is filled with data as the program runs. This all works fine, but I want to also then do a certain process on this data at the same...
5
by: BK | last post by:
We've got a fairly large scale development process under way in .NET 2003. We are about a month away from go-live for phase 1, second phase is rather short and all work should be completed in the...
9
by: Gummy | last post by:
Hello, I created a user control that has a ListBox and a RadioButtonList (and other stuff). The idea is that I put the user control on the ASPX page multiple times and each user control will...
4
by: emma_middlebrook | last post by:
Hi Advice needed about what's the best in the following situation. In essence, I have a GUI that needs to detail time taken to do jobs that execute in their own thread. Currently, the GUI...
6
by: Joe | last post by:
I've been getting this message at all different times since a few days ago. I understand the message but not why I get it. I don't have any other threads. This happens when closing dialog boxes,...
3
by: stumorgan | last post by:
Basically what I have is a form with a graph on it which graphs data that I'm reading from a USB device at 100 Hz (every 10ms). I have a thread reading and parsing the data from the USB, but when...
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: 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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.