473,387 Members | 1,572 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

event not raising

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.
Dec 13 '05 #1
9 1316
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
-------------------
"JamesB" <ja***@spamiscrappy.puffle.co.spam.uk> wrote in message
news:43***********************@news.zen.co.uk...
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.

Dec 13 '05 #2
"JamesB" <ja***@spamiscrappy.puffle.co.spam.uk> schrieb
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

Dec 13 '05 #3

"Ken Tucker [MVP]" <vb***@bellsouth.net> wrote in message
news:O$**************@TK2MSFTNGP10.phx.gbl...
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
Dec 13 '05 #4
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

Greg

"JamesB" <ja***@spamiscrappy.puffle.co.spam.uk> wrote in message
news:43***********************@news.zen.co.uk...
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.

Dec 13 '05 #5
"Greg Burns" <bl*******@newsgroups.nospam> schrieb:
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.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

Dec 13 '05 #6
"Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
... 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
Dec 13 '05 #7
"Greg Burns" <bl*******@newsgroups.nospam> schrieb:
... 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.


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.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

Dec 13 '05 #8
"Greg Burns" <bl*******@newsgroups.nospam> schrieb
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
Dec 13 '05 #9

"JamesB" <ja***@spamiscrappy.puffle.co.spam.uk> wrote in message
news:43***********************@news.zen.co.uk...

"Ken Tucker [MVP]" <vb***@bellsouth.net> wrote in message
news:O$**************@TK2MSFTNGP10.phx.gbl...
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


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.
Dec 14 '05 #10

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

Similar topics

5
by: Santhoshi | last post by:
I have an event declared in class1 Now i want to raise the event in the my class2 or anywhere other than class1 Is it possible? If so how Thank you
6
by: Dan | last post by:
I've created a pocketpc app which has a startup form containing a listview. The form creates an object which in turn creates a System.Threading.Timer. It keeps track of the Timer state using a...
6
by: Steve B. | last post by:
Is it good programming practice to call an event within an event? I'm I creating recursion somehow somewhere? Does an event (e.g. send, e) ever end? I'm sorry, I'm not sure I know what I mean,...
4
by: rawCoder | last post by:
Hi all, How Can You Raise Events Asynchronously ? Now for the details ... I want to do inter modular communication using events in such a way that the contributing modules need not...
3
by: Edward Diener | last post by:
According to the CLS specification, the accessibility of the methods for adding, removing, and raising an event must be identical. There appear to be a few problems with this: 1) According to...
3
by: bclegg | last post by:
Hi, I am trying to use a 3rd Party telephony (Intel's CT-ADE 8.3) library in a vb.net service. The way it hangs up is to raise an Event. If you build a windows Application you can write: Sub...
4
by: Charles Law | last post by:
Suppose a worker thread needs to signal to the main thread that an event has occurred. Ordinarily, any event raised by the worker thread will be on its own thread. How can the worker thread...
3
by: George | last post by:
Hi, I have searched on the net quite a bit, but the more I read, the more confused I get. I want to see if I can raise an event out side of the class. I believe this can be done in VB (at...
4
by: sloan | last post by:
I"m trying to figure out what concept I'm missing here, or if its not a good idea .. or what. Here is my example.. code is below. I have an employee class. It has an event that can be raised....
1
by: Phil Townsend | last post by:
I have an application that needs to respond to events that occur outside of the application itself. My project, called "ShowDetection" declares the event. I have a console app called "TestEvent"...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.