473,785 Members | 3,417 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Best way to handle Control.InvokeR equired without duplicating code?


I'm trying to find a good way to handle Control.InvokeR equired 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(Ad dressOf 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 InvokeIfRequire d 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.GetInvo cationList()

Dim target As Object = iDelegate.Targe t
If Not target Is Nothing AndAlso _
TypeOf target Is Control AndAlso _
DirectCast(targ et, Control).Invoke Required Then

If synchronous Then
DirectCast(targ et, Control).Invoke (iDelegate, args)
Else
DirectCast(targ et, Control).BeginI nvoke(iDelegate , args)
End If
Else
iDelegate.Dynam icInvoke(args)
End If
Next
End Sub
Nov 21 '05 #1
2 4087
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(By Val Sender As Object, _
ByVal e As System.EventArg s)
If Me.InvokeRequir ed Then
Dim del As New UpdateUIFilesFo undDelegate( _
AddressOf Me.UpdateUIFile sFound)
Me.BeginInvoke( del
Else
Me.UpdateUIFile sFound()
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(By Val Sender As Object, _
ByVal e As System.EventArg s)
RaiseEvent FilesFound(Send er,e)
End Sub

Private Sub OnSafeFilesFoun d(ByVal Sender As Object, _
ByVal e As System.EventArg s)
If Me.InvokeRequir ed 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(By Val Sender As Object, _
ByVal e As System.EventArg s)
If Me.InvokeRequir ed Then
Dim del As New UpdateUIFilesFo undDelegate( _
AddressOf Me.UpdateUIFile sFound)
Me.BeginInvoke( del
Else
Me.UpdateUIFile sFound()
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(By Val Sender As Object, _
ByVal e As System.EventArg s)
RaiseEvent FilesFound(Send er,e)
End Sub

Private Sub OnSafeFilesFoun d(ByVal Sender As Object, _
ByVal e As System.EventArg s)
If Me.InvokeRequir ed 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
1589
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., in this language, the following code is possible: { Int32 i=0; OnEvent ( ... ) {
11
1901
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 other handles a custom "CardInserted" event for a class I wrote that watches for smart cards to be inserted into an attached smart card reader.
0
353
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 Invoke(new EventHandler(AddressOf thisFunc), new Object() { sender, e}) Return End If
3
1447
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 time. Because of the way the data is received into the control (from an event out of my hands) my processing causes problems as it is time-intensive. So, I figure my processing should go in a separate thread so it can work without stopping the...
5
2532
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 next 2 months. Looking back on problems encountered, we want to learn from this project. FWIW, we are nearly on time with the original time line (only off by about a month), and we actually added more functionality than the original specs...
9
3193
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 load with different data (locations, departments, etc.).
4
2233
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 thread instantiates a class that wraps a job, handing it a callback (delegate) to call once the job has finished. On callback, always on a different thread than the GUI thread,
6
32247
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, doing a drag and drop and a few other things which I don't remember right now. It only happens while I'm running within the IDE and only seems to be on my computer. We've tested the same thing on other machines running VS.2005 and there is no...
3
4154
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 it comes time to draw that data on the graph the work is handled on the main UI thread through a callback delegate since the form owns the graph control. Is there a way I can have a separate thread own the graph control and handle the drawing so...
0
9643
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9480
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10147
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9947
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8968
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6737
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5511
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4045
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3645
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.