473,320 Members | 1,828 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,320 software developers and data experts.

Withevents work AddHandler dont?

Hi,

Im having some trouble getting Addhandler to work. Using withevents
everything is working fine. But with AddHandler I get no callback.

The code below works fin if I declare _tcp as a private class member (etc.
Dim WithEvents mTcp as New MyTCP). Classes/modules; module Main, class
MyCtrl and class MyTcp.

Any tips why using AddHandler dont work?

/Vemund
Adding handler (from MyCtrl):
---
Dim _tcp As New MyTcp
_tcp.Init(ip, port)
AddHandler _tcp.OnDataArrival, AddressOf TcpHandler
---

Handler (from MyCtrl):
---
Public Sub TcpHandler(ByVal data As String)
RaiseEvent OnDataArrival(data)
End Sub
---

Rasing event (from MyTcp):
---
Public Sub ReadHandler(ByVal asyncResult As IAsyncResult)
Dim data As String = String.Empty
Dim bytes As Int32 = mNetStream.EndRead(asyncResult)
data = Encoding.ASCII.GetString(mReadbuffer, 0, bytes)
Console.WriteLine("Received: {0}", Data)
RaiseEvent OnDataArrival(data)
Dim result As IAsyncResult = mNetStream.BeginRead(mReadbuffer, 0,
mReadbuffer.Length, ReadCallback, Nothing)
End Sub
---
Aug 17 '06 #1
3 1642
Perhaps by the time you attach the handler, the event has aleady fired? Does
Init raise the event initially? Try adding the handler first, then calling
Init.

"Vemund Halvorsen" <ju**@mail.comwrote in message
news:1o*****************************@40tude.net...
Hi,

Im having some trouble getting Addhandler to work. Using withevents
everything is working fine. But with AddHandler I get no callback.

The code below works fin if I declare _tcp as a private class member (etc.
Dim WithEvents mTcp as New MyTCP). Classes/modules; module Main, class
MyCtrl and class MyTcp.

Any tips why using AddHandler dont work?

/Vemund
Adding handler (from MyCtrl):
---
Dim _tcp As New MyTcp
_tcp.Init(ip, port)
AddHandler _tcp.OnDataArrival, AddressOf TcpHandler
---

Handler (from MyCtrl):
---
Public Sub TcpHandler(ByVal data As String)
RaiseEvent OnDataArrival(data)
End Sub
---

Rasing event (from MyTcp):
---
Public Sub ReadHandler(ByVal asyncResult As IAsyncResult)
Dim data As String = String.Empty
Dim bytes As Int32 = mNetStream.EndRead(asyncResult)
data = Encoding.ASCII.GetString(mReadbuffer, 0, bytes)
Console.WriteLine("Received: {0}", Data)
RaiseEvent OnDataArrival(data)
Dim result As IAsyncResult = mNetStream.BeginRead(mReadbuffer, 0,
mReadbuffer.Length, ReadCallback, Nothing)
End Sub
---

Aug 17 '06 #2

"Vemund Halvorsen" <ju**@mail.comwrote in message
news:1o*****************************@40tude.net...
Hi,

Im having some trouble getting Addhandler to work. Using withevents
everything is working fine. But with AddHandler I get no callback.

The code below works fin if I declare _tcp as a private class member (etc.
Dim WithEvents mTcp as New MyTCP). Classes/modules; module Main, class
MyCtrl and class MyTcp.

Any tips why using AddHandler dont work?

/Vemund
Adding handler (from MyCtrl):
---
Dim _tcp As New MyTcp
_tcp.Init(ip, port)
AddHandler _tcp.OnDataArrival, AddressOf TcpHandler
---
Sounds to me like the _tcp variable goes out of scope (at which point the
event handler is implicitly removed) before the event is fired. You don't
need the WithEvents keyword btw, unless you attach event handlers by using
the Handles keyword. Simply declare you _tcp variable as a private clas
member like this:
Private _tcp As New MyTCP
/claes
Aug 17 '06 #3
On Thu, 17 Aug 2006 12:56:28 +0200, Vemund Halvorsen wrote:
Any tips why using AddHandler dont work?
Found the problem. I hade declared a ManualResetEvent object with WaitOne
inside the _tcp object. This blocks the thread. But when I use WithEvents
keyword it works (strange!).

I removed the ManualResetEvent object and everything works perfect - also
when I add the handler using AddHandler dynamically.

/Vemund
Aug 22 '06 #4

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

Similar topics

1
by: Rob Smeets | last post by:
Hi you Gurus, I would like your help with my 'challenge' ;-) Any help or advice would be greatly appriciated!! I've created a Class wich retrieves FTP files. In that Class there are several...
6
by: | last post by:
Is there any additional overhead for declaring a variable using the WithEvents keyword even if none of the events are being handled?
8
by: Bob Day | last post by:
Using VS 2003, VB.NET, MSDE If an event is raised in a class that was NOT instantiated with the WithEvents key word, is that event effectively ignored? Or, another way to phrase the question,...
8
by: DareDevil | last post by:
Class MyClas Friend ReadOnly WithEvents MyObject as AnotherClas .. End Clas I have a VB.NET class that has a readonly object (i.e. It can only be assigned in the constructor). This enables me...
3
by: Terry Olsen | last post by:
I want to have an array of class objects that raise an event when a condition is true. Like so... ------------------------------------------------------- Public Class ClientHandler Public...
2
by: DiGa | last post by:
Hi all I have to write an application in VB.NET which monitors multiple files at the time and notifies the user about changes. I thought to use multiple instances of FileSystemWatcher in an...
1
by: Joel Whitehouse | last post by:
Is there any way I can get the effects of declaring objects using WithEvents while also having indexed addressing? I'm designing a control that has several buttons, the functions of which vary...
6
by: Water Cooler v2 | last post by:
Am I right in guessing that there is no direct C# equivalant for the 'WithEvents' VB keyword. The substitute is to add event handlers to the delegate instances that are the public members of the...
4
by: =?Utf-8?B?R3V5IENvaGVu?= | last post by:
Hi all I googled some and found that I can no longer - dim withevents myclassname() I also understand that I can use addhandler... But... what if I do not know the amount of classes that will be...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

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.