473,799 Members | 3,267 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

delegate handling, or incorrect syntax... either way how can i do this?

Given the following code,

Public class ProgressFormMan ager
Public Sub BeginInvoke(ByV al method As [Delegate], ByVal args() As Object)
Dim asyncCallback As New System.Threadin g.WaitCallback( method)
System.Threadin g.ThreadPool.Qu eueUserWorkItem (asyncCallback, args)
End Sub
.... [other implementation details]
End Class

[in the Form codebehind]
Private Sub Button1_Click(. ..
Dim pfm as new ProgressFormMan ager(Me) 'Me is the form to become the
owner
pfm.BeginInvoke (addressof LongMethodCallb ack, new object() { arg1, arg2,
arg3 })
End Sub

Private Sub LongMethodCallb ack(byval param as object)
Dim args() as object = DirectCast(para m, object())
'do lengthy work
End Sub

since ProgressFormMan ager attempts to keep the form created on the same
thread as the caller and then fork a worker thread, I'm using the
QueueUserWorkIt em

problem follows:
A) Vb compiler fails on in ProgressFormMan ager.BeginInvok e with two errors:
1) 'System.Threadi ng.WaitCallback ' is a delegate type. Delegate construction
permits only a single AddressOf expression as an argument list. Often an
AddressOf expression can be used instead of a delegate construction.
2) 'AddressOf' expression cannot be converted to 'System.Delegat e' because
'System.Delegat e' is not a delegate type.

any ideas?

I know in C# this wouldnt even be a problem.

--
Eric Newton
C#/ASP Application Developer
http://ensoft-software.com/
er**@cc.ensoft-software.com [remove the first "CC."]
Nov 20 '05 #1
1 1168
On 2004-01-05, Eric Newton <er**@cc.enso ft-software.com> wrote:
Given the following code,

Public class ProgressFormMan ager
Public Sub BeginInvoke(ByV al method As [Delegate], ByVal args() As Object)
Dim asyncCallback As New System.Threadin g.WaitCallback( method)
System.Threadin g.ThreadPool.Qu eueUserWorkItem (asyncCallback, args)
End Sub
... [other implementation details]
End Class
You have to declare method as an actual delegate type. You may as
well use the WaitCallback type, since that's what you want anyway.

Public Sub BeginInvoke(ByV al method as Threading.WaitC allback)
ThreadPool.Queu eUserWorkitem(m ethod, args)
End Sub

And back in the form, it's simply

pfm.BeginInvoke (addressof LongMethodCallb ack)

(I'm not showing the args argument to avoid linebreaks, but obviously
just add that back in).
I know in C# this wouldnt even be a problem.


Actually, you can't pass System.Delegate s around like this in c# either.
In c#, you'd need to explicitly instantiate the delegate when you called
BeginInvoke. VB.Net will do that for you, but you have to let it know
what kind of delegate to create. You can either do that in the function
declaration, as above, or explicitly create the delegate when you call
the function.
--
David
dfoster at
hotpop dot com
Nov 20 '05 #2

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

Similar topics

1
2041
by: 0to60 | last post by:
More importantly, WHY do I wanna do this? Well, I have events that I raise asynchronously like this: foreach(eventhandler handler in eventname.getinvocationlist()) handler.begininvoke(); Now, I'd like to put a callback in there so I can call endinvoke and catch any exceptions that get thrown in event handling code. Now, to call endinvoke, you have to save the IAsynchResult returned by begininvoke, and
11
2340
by: Yoni Rabinovitch | last post by:
Is it possible to invoke a C# delegate/event handler asynchronously, from unmanaged C++ ? I assume this requires a Managed C++ wrapper, which would call BeginInvoke on the delegate ? Is this correct, and if so, does anyone have an example ? Thanks !!
7
1794
by: Brian Keating | last post by:
hi there this may seem like a silly question but what exactly is the difference ... ok .. that question is probably a bit ambiguous.. i know what delegates are i know what events are, i use them everyday but.. what are events needed? don't delegates do everything? look at this code normally i would use an event but i've just stuck with delegate in this instance and it still works! i'm baffelled! namespace DelegateTst
8
2393
by: Lachlan Hunt | last post by:
Hi, I'm interested in finding out how erroneous comment syntax within an HTML document should be handled by a parser, according to SGML rules. At present, every browser handles comments in different ways, with only Mozilla and very recent builds of a few others browsers have anything at least closely resembling proper SGML comment handling. In particular, take this comment for example: <!-- Hello -- World -- ! -->
3
1406
by: lothar.behrens | last post by:
Hi, I am thinking about the delegate mechanism and try to understand it. I am coming from C++ and know about callbacks or member callbacks. In C++ I have this typedef for every class that implements member callbacks: typedef bool (__cdecl IEventHandler::*lbEvHandler)(IUnknown* uk);
11
1834
by: ohmmega | last post by:
hello world. i would like to implement a class with a timer, witch informs me every second about it's tick. the code works already, but i would like to change a thing (or more). <code> //at the moment i initialize the object with the constructor: public myClass(System.Timers.ElapsedEventHandler CallMeBack) {
7
1422
by: colin | last post by:
Hi, How can I use a delegate that I can set to call a non static function but of any instance of the class ? eg class TypeTeader<T> { delegate T readDelegate<T>();
6
1405
by: damiensawyer | last post by:
Hi, Can someone please explain to me something about delegates? My understanding is as follows. A delegate is basically an object that can hold a reference to a "method" somewhere. That is, it's essentially a pointer to a piece of code somewhere else in memory. It therefore (to me anyway) makes sense to define delegates with their signatures and be able to use those signatures at different points in
7
2727
by: Stefan Hoffmann | last post by:
hi @all, is there something like an anonymous delegate? This is the original code: -- private delegate void DelegateSetFormCaption(string text); private void SetFormCaption(string text) {
0
9688
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
9546
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
10490
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10243
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10030
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
9078
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
6809
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();...
1
4146
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
3762
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.