Passing function pointer in VB.Net

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Coming from a C/C++ background, how would I pass a function pointer to a
function? I want to write a function that handles certain thread spawning.
Here's what I'm trying to invision:

function( thesub as <functionptr?> )
dim t as new system.threading.thread( _
new system.threading.threadstart( Addressof thesub ))
....

How can I get something like that going in VB.Net?
 
Use delegates. Delegates are the equivalent of function pointers in .NET.



Regards,



Ron
 
I tried using delegates but I don't know how. I tried something like this:

delegate function fpMyfunc()
(I also tried delegate sub fpMyfunc())

Then:
function( thesub as fpMyfunc )
dim t as new system.threading.thread( _
new system.threading.threadstart( Addressof thesub ))

But this did not work. VB.Net complained about the "thesub" after the
Addressof.. saying that I do not need ()'s after the sub name, as if it did
not recognize "thesub" as being a subroutine.
 
So.. any help here? Anyone else got delegates working as a parameter for a
function to be used with an AddressOf operator?
 
ThreadStart wants the name of the actual method that's being executed
because it's actually creating a delegate as well, and I don't think you
can have a delegate of a delegate. Here's what I came up with that
might meet your needs:

Private Sub ThisTest()
'Do Something
End Sub

Private Function StartAThread(ByVal thisSub As Threading.ThreadStart)
Dim t as New Threading.Thread(thisSub)
End Function

Private Sub MyTest()
Dim ts as New Threading.ThreadStart(AddressOf ThisTest)
SomeName(ts)
End Sub

'ThisTest' is the routine you are wanting to run on a separate thread.
'StartAThread' is the routine that actually creates the new thread an
excutes the passed method to it.
'MyTest' is the routine that is running a finds a method that needs to
be spawned on a separate thread.

All you have to do is create the ThreadStart object and pass that to the
'StartAThread' function and everything should work.

Hopefully this helps,
Brian Swanson
 
ChrisB.
Think of "AddressOf" as New for Delegates, you only need to use it when you
want to create a new delegate, not when you already have an existing
Delegate.

Define your function as taking a ThreadStart parameter, when you call your
function use AddressOf, when you create the Thread itself you do not need
AddressOf as you already have a Delegate.

Something like:

Imports System.Threading

Private Sub Worker()
' do work here...
End Sub

Private Sub MoreWork()
' do more work here...
End Sub

Private Sub SubmitWork(ByVal worker As ThreadStart)
Dim theThread As New Thread(worker)
theThread.Start()
End Sub

Public Sub Main()
SubmitWork(AddressOf Worker)
SubmitWork(AddressOf MoreWork)
End Sub

Notice that when we call SubmitWork we use AddressOf to create a new
Delegate. Then when we create a new Thread within SubmitWork, we simply pass
that Delegate directly to the constructor.

Hope this helps
Jay
 
Aha. OK thank you very much, Brian and Jay. This sounds like what I need to
know. I will try it.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top