actions done twice but only one trigger?

B

Ben

I am sending a packet to a server when an event is triggered. When the
event is triggered (only once) I get two packets sent. I am supposed to only
get one packet. I used the MS SDK as the basis for this program. What am I
doing wrong?

Thanks,
Ben

Namespace Microsoft.Samples
Module LogMonitor
Dim WithEvents evtSec As New EventLog("Security")

Public Sub Main()
evtSec.MachineName = "."
AddHandler evtSec.EntryWritten, AddressOf OnEntryWritten
evtSec.EnableRaisingEvents = True
While (Console.Read() <> 113)
System.Threading.Thread.Sleep(500)
End While
End Sub

Sub OnEntryWritten(ByVal source As Object, ByVal e As
EntryWrittenEventArgs) Handles evtApp.EntryWritten, evtSec.EntryWritten,
evtSys.EntryWritten
Try
sendinfo(e, "192.168.1.33", "44000")
Catch se As Exception
MsgBox(se.ToString(), , "LogMonitor Error")
End Try
End Sub

Sub sendinfo(ByVal e As EntryWrittenEventArgs, ByVal servername As
String, ByVal portnum As String)
Dim sendbuffer As [Byte]()
Dim sendstring As String
Dim hostname As String = System.Net.Dns.GetHostName()
Dim tcpClient As New System.Net.Sockets.TcpClient()
tcpClient.Connect(servername, portnum)
Dim networkStream As System.Net.Sockets.NetworkStream =
tcpClient.GetStream()
sendstring = "Check my security log"
sendbuffer = System.Text.Encoding.ASCII.GetBytes(sendstring)
networkStream.Write(sendbuffer, 0, sendbuffer.Length)
tcpClient.Close()
End Sub
End Module
End Namespace
 
B

Ben

Ben said:
I am sending a packet to a server when an event is triggered. When the
event is triggered (only once) I get two packets sent. I am supposed to
only get one packet. I used the MS SDK as the basis for this program.
What am I doing wrong?

Thanks,
Ben

I don't know what the problem was, but coping the code to a new project
fixed it.
 
C

Chris Dunaway

Ben said:
I am sending a packet to a server when an event is triggered. When the
event is triggered (only once) I get two packets sent. I am supposed to only
get one packet. I used the MS SDK as the basis for this program. What am I
doing wrong?

It looks like you are wiring up the event handler twice. Once in Sub
Main where you call AddHandler and again when you declare the
OnEntryWritten sub using the Handles keyword. You don't need both.
Either use WithEvent and the Handles keyword, or use AddHandler alone.

Public Sub Main()
evtSec.MachineName = "."
AddHandler evtSec.EntryWritten, AddressOf OnEntryWritten

First event handler wired up here: ^^^^^

End Sub

Sub OnEntryWritten(ByVal source As Object, ByVal e As
EntryWrittenEventArgs) Handles evtApp.EntryWritten, evtSec.EntryWritten,
evtSys.EntryWritten

Second event handler wired up here using the Handles keyword: ^^^
 

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