SaloTheSolo

i have some confusing matter i wana ask about :

am using delegate to pass a function as a parameter to another function but am having a prob

my function passed cant access the form parts(listboxes,tables,.....)

first i defined the function as shared and pass it but it doesnt access

then i defined it as friend and make an instance of the defining class to pass it to the delegate but also the same result

example 1:

// the delegate

Public Delegate Sub LinkProcAddr(ByRef LinkState As LinkStateStruct)

// the function

Friend Sub HostLinkStateChange(ByRef data As LinkStateStruct)

Form1.CallbacksListBox.Items.Add("anything data. ......")

End Sub

// knowing that the fuction is a callback that is triggered automaticly by the fuction in the form1:

Dim LPAPLC As LinkProcAddr = AddressOf fts.HostLinkStateChange

ptRegisterLinkStateCallback(CallbackTypes.ptHostLinkStateChange, LPAPLC)

..............................................................................................................................

example 2:

Public Delegate Sub LinkProcAddr(ByRef LinkState As LinkStateStruct)

Shared Sub HostLinkStateChange(ByRef data As LinkStateStruct)

Form1.CallbacksListBox.Items.Add("anything data. ......")

End Sub

Dim LPAPLC As LinkProcAddr = AddressOf HostLinkStateChange

ptRegisterLinkStateCallback(CallbackTypes.ptHostLinkStateChange, LPAPLC)

.........................................................................................................

(How can i access the form)

so please if anyone can answer me il be soo gratefull

thanks in advance




Re: Common Language Runtime Delegates .........

nobugz

HostLinkStateChange ought to be a method of the Form1 class. If that's a problem, keep your fingers crossed that "Form1" will actually reference the existing instance of the form and make sure CallbacksListBox's Modifiers property is set to Public. If that's not close than the actual error message would be very helpful to know.





Re: Common Language Runtime Delegates .........

SaloTheSolo

first thanks for all ur answers the nswer conserning the callback b4 this question was too much helpful but this one i dont guess that u understand me i have a class containing a function shared that is called by another one using delegates

but that function cant access the main form that have a list box i want to return result intothat list box

the problem that the function is called automaticly from an unmanaged dll so i dont know when its triggered

i dont know if am clear but thats my prob and thanks much realy u r so helpfull






Re: Common Language Runtime Delegates .........

nobugz

Please explain why the callback function can't be a member of Form1.





Re: Common Language Runtime Delegates .........

SaloTheSolo

My aim is to make a component containing all the functions and the call backs registration and functions where it will be a user drag and drom and am aiming to do the callback functions as modifiable events but this is next stage so it must be separated from the form calling it moreover i tried to define it in the form it self but also it cant access the listbox cz the caller is a function found outside in the dll is the caller so no access to the form.........






Re: Common Language Runtime Delegates .........

nobugz

Well, you component then better needs a reference to the form. Pass it in the constructor.





Re: Common Language Runtime Delegates .........

SaloTheSolo

to clarify the prob here's the schema:

\\delegate is defined in the dll wrapper which also contains the ptRegisterLinkStateCallback function

Public Delegate Sub LinkProcAddr(ByRef LinkState As DemoApiStructures.LinkStateStruct)

//my form

Public Class Form1

Dim LPAPLC As LinkProcAddr = AddressOf HostLinkStateChange

Public Sub HostLinkStateChange(ByRef data As LinkStateStruct)

Dim str As String = String.Format("Host link state change prev={0} now={1}", data.OldState, data.NewState)

CallbacksListBox.Items.Add(str)

End Sub

Private Sub RegisterCallbackButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RegisterCallbackButton.Click

tRegisterLinkStateCallback(CallbackTypes.ptHostLinkStateChange, LPAPLC)// this function is defined in another class marshalled from the dll file

ptReady()//this is the trigger of the function of the call back but theres no list adding

EndSub

thats my prob how can i solve it there no error message but there is no display of the callback text






Re: Common Language Runtime Delegates .........

nobugz

Okay, looks good. Now, what does ptReady() do





Re: Common Language Runtime Delegates .........

SaloTheSolo

ptReady is a function that intialize the dll and send back a host link status (change it from 2 to 3)

the body of this function i dont know it cz its in the dll soo i know that it sets the ready status of the dll

thanks for following............






Re: Common Language Runtime Delegates .........

nobugz

Is this callback actually called by the DLL Set a breakpoint to diagnose. Is it being called on another thread Use Control.Invoke() if that's the case.





Re: Common Language Runtime Delegates .........

SaloTheSolo

yes the callback is called by the dll and on another thread their is no control defining it inorder to invoke the event

its just triggered automaticly

the prob is that i can print inside the function but i cant send it to the form i make a sln by adding a shared collection os strings and sending the messages to it then after a period i click a button the actually sets the datasource of the desired list to the collection and its showing the results

but that is synchronous i need to be asynchronious

...........






Re: Common Language Runtime Delegates .........

nobugz

You'll have to use Control.Invoke() or BeginInvoke(). Sure you've got a control instance to do this: both "Me" and "CallbacksListbox" in HostLinksStateChange.





Re: Common Language Runtime Delegates .........

SaloTheSolo

Realy thanks for your help i dont want to be so pushy but i dont want to invoke the callback its invoked automaticly i just wanted to give the info to the form when the callback is invoked........




Re: Common Language Runtime Delegates .........

nobugz

I guess what you're asking is that you want to marshal the callback in your own class so that the form doesn't have to bother with it. To do that, you'll again need a reference to the form in your class. A workaround that's not 100% reliable but usually works is to use Application.OpenForms[0] as a form reference to make the Invoke call.





Re: Common Language Runtime Delegates .........

SaloTheSolo

sory it didnt work any other suggession

i tried almost everything but it didnt work

send me a small piece of code plz about a class accessing the form asychrounously and thanks a lot