bakershawnm
Programmer
I am writing a service that spawns a thread. The thread listens at a socket. I would like the thread to write to the event log. I know how to handle it in VB.NET using the WithEvents.
The problem I am getting is when the thread tries to write to the event log the service (and thread) crashes.
If I have to handle it similar to the way VB.NET does and Raise the event back to the service and let the service write it to the log how do I do that since C# does not have WithEvents?
If I have to do it another way how do I do it?
I am using VS 2008 on Win 7 and .Net 3.5 on Server 2003. The service worked fine until I added the event log.
The last line is where it crashes
using System;
using System.Net;
using System.Net.Sockets;
using System.Collections.Generic;
using System.Data.Linq;
using System.Text;
using System.Diagnostics;
namespace SyncSocket
{
public class SynchronousSocketListener
{
// Incoming data from the client.
public static string data = null;
public static void StartListening()
{
EventLog evntlog = new EventLog("Application");
// Data buffer for incoming data.
byte[] bytes = new Byte[1024];
// Establish the local endpoint for the socket.
// Dns.GetHostName returns the name of the
// host running the application.
IPHostEntry ipHostInfo = Dns.GetHostEntry(Dns.GetHostName());
IPAddress ipAddress = ipHostInfo.AddressList[0];
IPEndPoint localEndPoint = new IPEndPoint(ipAddress, 11000);
// Create a TCP/IP socket.
Socket listener = new Socket(AddressFamily.InterNetwork,
SocketType.Stream, ProtocolType.Tcp);
// Bind the socket to the local endpoint and
// listen for incoming connections.
try
{
listener.Bind(localEndPoint);
listener.Listen(10);
// Start listening for connections.
while (true)
{
//Console.WriteLine("Waiting for a connection...");
// Program is suspended while waiting for an incoming connection.
evntlog.WriteEntry("Waiting for a connection...");
Any help will be much appreciated.
Shawn
The problem I am getting is when the thread tries to write to the event log the service (and thread) crashes.
If I have to handle it similar to the way VB.NET does and Raise the event back to the service and let the service write it to the log how do I do that since C# does not have WithEvents?
If I have to do it another way how do I do it?
I am using VS 2008 on Win 7 and .Net 3.5 on Server 2003. The service worked fine until I added the event log.
The last line is where it crashes
using System;
using System.Net;
using System.Net.Sockets;
using System.Collections.Generic;
using System.Data.Linq;
using System.Text;
using System.Diagnostics;
namespace SyncSocket
{
public class SynchronousSocketListener
{
// Incoming data from the client.
public static string data = null;
public static void StartListening()
{
EventLog evntlog = new EventLog("Application");
// Data buffer for incoming data.
byte[] bytes = new Byte[1024];
// Establish the local endpoint for the socket.
// Dns.GetHostName returns the name of the
// host running the application.
IPHostEntry ipHostInfo = Dns.GetHostEntry(Dns.GetHostName());
IPAddress ipAddress = ipHostInfo.AddressList[0];
IPEndPoint localEndPoint = new IPEndPoint(ipAddress, 11000);
// Create a TCP/IP socket.
Socket listener = new Socket(AddressFamily.InterNetwork,
SocketType.Stream, ProtocolType.Tcp);
// Bind the socket to the local endpoint and
// listen for incoming connections.
try
{
listener.Bind(localEndPoint);
listener.Listen(10);
// Start listening for connections.
while (true)
{
//Console.WriteLine("Waiting for a connection...");
// Program is suspended while waiting for an incoming connection.
evntlog.WriteEntry("Waiting for a connection...");
Any help will be much appreciated.
Shawn