RaiseEvent & AddHandler Troubles

I’m testing a tcp socket and cannot figure out why an event that I set to RaiseEvent is called but breakpoints in the event method are never reached.

Can ya’ll have a look and see if anything jumps out?

[code]//connect the socket
me.Connect

//while the socket isn’t connected
While Not me.IsConnected
//check to see if the socket got an error
If me.LastErrorCode <> 0 then
dim d as new date
MakeDebugFile(d.SQLDateTime + ": Error: connecting to server: " + str(me.LastErrorCode)+ chr(13))

  RaiseEvent LoginAttempted(false) //<---- Breakpoint stops here, but not in the LoginAttempted method 
  me.Close
  
  Exit
End If
//poll the socket to let it do its thing
me.Poll

Wend

//if we broke the loop because we’re connected
If me.IsConnected then
//here would be a great place to do a synchronous read operation…
dim s as String
Else
dim d as new date
MakeDebugFile(d.SQLDateTime + ": Error: Lost connection to server " + str(me.LastErrorCode) + chr(13))

RaiseEvent LoginAttempted(false)  //<---- Breakpoint stops here, but not in the LoginAttempted method 
me.Close

Return

End If[/code]

In a window, the event is supposed to be fired via AddHandler but it doesn’t unless the socket connects properly :

dim imc as EmailClass Emc = new EmailClass(server, port, address, pw, ssl) AddHandler Emc.LoginAttempted, AddressOf EventLoginAttempted

Any help would be appreciated! :slight_smile:

Is the socket a property or a control? Are you sure you only have one socket in the window? Perhaps one defined in code and one as a property?

Is the code you’re showing there in the Constructor? If so, the AddHandler is not being called until AFTER all of that code runs.

RaiseEvent triggers the event in an subclass of the class, not in the class itself.

I’d just create a subclass then creates instances of that and attach handlers to those instance in code if I’m only going to use code to handle everything.

I now have it as a property thinking it was somehow going out of scope. It’s only one socket (none dragged to the window, all in code):

Emc = new EmailClass(server, port, address, pw, ssl) AddHandler Emc.LoginAttempted, AddressOf EventLoginAttempted

Sorry, I forgot to mention it is a subclass of SSLSocket.

Originally the code above was in a method that was called from Constructor (albeit it was the last thing to be executed in the constructor). But I followed that lead and moved it to the Error event handler of the SSLSocket SublClass:

If me.LastErrorCode = 103 then//really only interested in the error 103 for now RaiseEvent LoginAttempted(false) //<---- Breakpoint stops here, but not in the EventLoginAttempted method End If

[code]Private Sub EventLoginAttempted(sender as EmailClass, gotConnection as Boolean)

//FWIW, I can’t seem to get any of this to execute Unless, GotConnection = True
//GotConnection = False never works

if gotConnection then
//connected
else
//not connected
end if
End Sub[/code]

Same result though.

[quote=126459:@Norman Palardy]I’d just create a subclass then creates instances of that and attach handlers to those instance in code if I’m only going to use code to handle everything.

[/quote]

I think that is what I’m doing. But I’m not sure if thats coming through in my communication on here. Lol :slight_smile:

Put a break on the AddHandler line and the RaiseEvent line. Which one happens first?

RaiseEvent is happening before AddHandler

Then you’re trying to connect from within the Constructor. Don’t do that. Move that code outside the class and call it explicitly after you call AddHandler.

You could also pass your callback into the constructor and AddHandler there. This requires a delegate to be defined and may or may not impose an inflexibility in how you want to link up handlers

[code]Class EmailClass inherits SSLSocket

Event LoginAttempted(state As boolean)
Sub ()

Private Delegate Sub delLoginAttempted(sender As EmailClass, state As boolean)

Sub Constructor(loginCallback As delLoginAttempted = nil)

Super.Constructor

if loginCallback <> nil then
  AddHandler self.LoginAttempted, loginCallback
end

RaiseEvent LoginAttempted(false)

End Sub

End Class

Window Window1

Sub Action() //Pushbutton
dim em As new EmailClass(AddressOf EventLoginAttempted)
End Sub

Private Sub EventLoginAttempted(sender As EmailClass, state As boolean)
Msgbox "EventLoginAttempted: " + Str(state)
End Sub

End Window
[/code]

@Will Shank it looks like your idea would work so I can still connect automatically from the constructor.

@Tim Hare for simplicity’s sake, I ended up going with your suggestion. Which is also what I think @Greg O’Lone was communicating to me but I didn’t understand until now.

Thank you all for you help!