event not raising

J

JamesB

I want progress on my ftp upload, so I am trying to add an event.
All the FTP stuff is in its own class (FTPClient). In the class header I
have:

Public Event SendProgress(ByVal bytes As Integer)

and in the upload loop I have:

RaiseEvent SendProgress(TotalBytes)

In the forms part of the app, I have:

Public WithEvents FTPClient As FTPClient

and

Private Sub FTPClient_SendProgress(ByVal bytes As Integer) Handles
FTPClient.SendProgress
'occurs when send thingy receives event.
'do stuff here to tell the user what's happening
End Sub

however it doesn't work. If I step through the code, when I get tto the
raiseevent line in the loop nothing branches off or happens. What am I
missing; it's my first attempt at my own events!
Thanks
James.
 
K

Ken Tucker [MVP]

Hi,

Generally events should have 2 arguments sender as object and e as
eventargs. You could make a class that inhertits from eventargs to include
extra data. However your event should still fire. Could you please post
the code that raises the event.

Ken
 
A

Armin Zingler

JamesB said:
I want progress on my ftp upload, so I am trying to add an event.
All the FTP stuff is in its own class (FTPClient). In the class
header I have:

Public Event SendProgress(ByVal bytes As Integer)

and in the upload loop I have:

RaiseEvent SendProgress(TotalBytes)

In the forms part of the app, I have:

Public WithEvents FTPClient As FTPClient

Do you assign an object to *this* variable? Or maybe accidently to a local
variable only?


Armin
 
J

JamesB

Ken Tucker said:
Hi,

Generally events should have 2 arguments sender as object and e as
eventargs. You could make a class that inhertits from eventargs to
include extra data. However your event should still fire. Could you
please post the code that raises the event.

Ken

Thanks for the response Ken.

Code snippet below (I won't paste the whole function, it's quite long,
however the function does run as expected asides from the event problem)

dim TotalBytes as Integer

'Begin upload of data
Dim TheStream As Stream = Conn.GetStream
Dim StreamStatus As Boolean = TheStream.CanWrite
BytesRead = FStream.Read(bData, 0, 1024)
TheStream.Write(bData, 0, 1024)
TotalBytes = 1024
While BytesRead > 0
Try
BytesRead = FStream.Read(bData, 0, 1024)
TheStream.Write(bData, 0, BytesRead)
TotalBytes = TotalBytes + 1024
RaiseEvent SendProgress(TotalBytes)
Catch ex As Exception
MsgBox("Error sending data:" & vbCrLf & vbCrLf &
ex.GetBaseException.Message & vbCrLf & vbCrLf & "Stack Trace:" & vbCrLf &
ex.GetBaseException.StackTrace, MsgBoxStyle.Exclamation, "Error sending
data")
Exit While
End Try
End While
 
H

Herfried K. Wagner [MVP]

Greg Burns said:
Not sure if relevant to your problem, but sounds like something that
stumped me awhile back. Events cannot be raised inside constructors.

http://tinyurl.com/cjvfw

.... except if event handlers are added inside the constructor using
'AddHandler', or an object is assigned to a 'WithEvents' variable. So,
events can be raised, but in most scenarios event handlers are not yet
connected.
 
G

Greg Burns

Herfried K. Wagner said:
... except if event handlers are added inside the constructor using
'AddHandler', or an object is assigned to a 'WithEvents' variable. So,
events can be raised, but in most scenarios event handlers are not yet
connected.

Herfried,

Here is a really stripped down version of the code that tripped me up
before. Notice myClass1 "is" defined WithEvents, but the event doesn't get
raised during the constructor, only when I raise it from a method call.

Probably showing my stupidity here, but it was your post that helped work
around this problem before. Maybe you could shed some light on the issue
with this (contrived) code.

Thanks,
Greg

Public Class Form1

Private WithEvents myClass1 As Class1

Public Sub New()

' This call is required by the Windows Form Designer.
InitializeComponent()

' Add any initialization after the InitializeComponent() call.

myClass1 = New Class1 ' does NOT call MyEventHandler

myClass1.FooBar() ' does call MyEventHandler

End Sub

Private Sub MyEventHandler(ByVal o As Object, ByVal e As EventArgs)
Handles myClass1.SomeEvent
'do something
End Sub

End Class

Public Class Class1
Event SomeEvent(ByVal o As Object, ByVal e As EventArgs)

Public Sub New()
RaiseEvent SomeEvent(Me, New EventArgs)
End Sub

Public Sub FooBar()
RaiseEvent SomeEvent(Me, New EventArgs)
End Sub

End Class
 
H

Herfried K. Wagner [MVP]

Greg Burns said:
Here is a really stripped down version of the code that tripped me up
before. Notice myClass1 "is" defined WithEvents, but the event doesn't get
raised during the constructor, only when I raise it from a method call.

Probably showing my stupidity here, but it was your post that helped work
around this problem before. Maybe you could shed some light on the issue
with this (contrived) code.

Thanks,
Greg

Public Class Form1

Private WithEvents myClass1 As Class1

Public Sub New()

' This call is required by the Windows Form Designer.
InitializeComponent()

' Add any initialization after the InitializeComponent() call.

myClass1 = New Class1 ' does NOT call MyEventHandler

Yep. At this time the event handler ('WithEvents' /outside/ the class)
haven't been added yet.
 
A

Armin Zingler

Public Class Class1
Event SomeEvent(ByVal o As Object, ByVal e As EventArgs)

Public Sub New(ByVal Handler As SomeEventEventHandler)

Addhandler SomeEvent, Handler
RaiseEvent SomeEvent(Me, New EventArgs)
End Sub

Public Sub FooBar()
RaiseEvent SomeEvent(Me, New EventArgs)
End Sub

End Class


dim c as class1

c = new class1(addressof OnEvent)

sub onevent(...)
end sub


Now you raise an event in the constructor.


Armin
 
J

JamesB

JamesB said:
Thanks for the response Ken.

Code snippet below (I won't paste the whole function, it's quite long,
however the function does run as expected asides from the event problem)

dim TotalBytes as Integer

'Begin upload of data
Dim TheStream As Stream = Conn.GetStream
Dim StreamStatus As Boolean = TheStream.CanWrite
BytesRead = FStream.Read(bData, 0, 1024)
TheStream.Write(bData, 0, 1024)
TotalBytes = 1024
While BytesRead > 0
Try
BytesRead = FStream.Read(bData, 0, 1024)
TheStream.Write(bData, 0, BytesRead)
TotalBytes = TotalBytes + 1024
RaiseEvent SendProgress(TotalBytes)
Catch ex As Exception
MsgBox("Error sending data:" & vbCrLf & vbCrLf &
ex.GetBaseException.Message & vbCrLf & vbCrLf & "Stack Trace:" & vbCrLf &
ex.GetBaseException.StackTrace, MsgBoxStyle.Exclamation, "Error sending
data")
Exit While
End Try
End While

Thanks for everyone's help, I've got it working now following the example in
the EventArgs class in MSDN library (the "Fire alarm" sample!)

James.
 

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

Top