Newbie Question

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi,

I have a VB NET class library. In the constructor of the library I spawn off
a thread that is listening for some data. When it gets data it raises an
event.

Constructor has code that looks like the following ::

Private MyThread As Thread
MyThread = New Thread(AddressOf GetData)
MyThread.Start()

In the destructor I have the following code to get rid of the spawned off
thread ::

If (MyThread.ThreadState = ThreadState.Running) Then
MyThread.Abort()
MyThread.Join()
End If

The problem is this:: For a VB NET project using this class library
everything works.
But for C# the thread that was spawned off is not destroyed. This is the
only thing that does not seem to be working well with a C# project using this
class library.

Is there any difference in the way C# and VB NET handle threads?

Thanks in advance,

Pete.
 
Peter,

I wouldn't use Abort to terminate the thread. Rather, I would use
something that would send a signal which would tell the thread to get out of
the wait state that it is in, and then have it exit the thread without
having to bash it.

This would probably resolve the issue.

Hope this helps.
 
Nicholas,

I did try that approach. If you bear with me here I will outline it here. I
wrote a UDP socket wrapper. I have a GetData method that runs on a separate
thread that raises an event as follows::
Public Enum SocketStates
NotBound = 0
Bound = 1
End Enum

Private MyState As SocketStates

Private Sub GetData()
Dim GetSocket As New UdpClient(iLocalPort)
Dim RemoteComputer As New
System.Net.IPEndPoint(System.Net.IPAddress.Any, 0)
Dim MyByte() As Byte
Dim MyText As String

Do While MyState = SocketStates.Bound

MyByte = GetSocket.Receive(RemoteComputer)
MyText = ASCII.GetString(MyByte)
RaiseEvent DataArrival(MyText, MyByte,
RemoteComputer.Address.ToString, RemoteComputer.Port)

Loop

End Sub

This method loops for ever and raises an event whenever data comes in.

I have a cleanup method that called StopSocket that is invoked before the
application terminates.
Public Sub StopSocket()

If MyState = SocketStates.Bound Then
MyState = SocketStates.NotBound
SendText("", "LocalHost", iLocalPort)
End If

End Sub

Now before the application that instantiated the class library exits it
calls the StopSocket() method.

My problem is that this works fine if the project using this class library
is a VB NET project. But with a C# project the thread never terminates
gracefully. Thats why I was trying to kill the thread as part of StopSocket().

I posted this here because I am trying to get this library to work with a C#
project.

Any thoughts on this will be appreciated. Thanks in advance.

Pete.

Nicholas Paldino said:
Peter,

I wouldn't use Abort to terminate the thread. Rather, I would use
something that would send a signal which would tell the thread to get out of
the wait state that it is in, and then have it exit the thread without
having to bash it.

This would probably resolve the issue.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Peter Krikelis said:
Hi,

I have a VB NET class library. In the constructor of the library I spawn
off
a thread that is listening for some data. When it gets data it raises an
event.

Constructor has code that looks like the following ::

Private MyThread As Thread
MyThread = New Thread(AddressOf GetData)
MyThread.Start()

In the destructor I have the following code to get rid of the spawned off
thread ::

If (MyThread.ThreadState = ThreadState.Running) Then
MyThread.Abort()
MyThread.Join()
End If

The problem is this:: For a VB NET project using this class library
everything works.
But for C# the thread that was spawned off is not destroyed. This is the
only thing that does not seem to be working well with a C# project using
this
class library.

Is there any difference in the way C# and VB NET handle threads?

Thanks in advance,

Pete.
 
Peter,

I think that it has little to do with the fact that it is VB. I would
first define a property which indicates whether or not the polling should
continue.

// The value from the property.
private bool enabled = false;

public bool Enabled
{
[MethodImpl(MethodImplOptions.Synchronized)]
get
{
// Return the value.
return enabled;
}
[MethodImpl(MethodImplOptions.Synchronized)]
set
{
// Just set the value.
enabled = false;
}
}

You use the MethodImpl attribute here so that when you access the value
on different threads, you won't have a race condition.

GetData then looks like this:

Private Sub GetData()
Dim GetSocket As New UdpClient(iLocalPort)
Dim RemoteComputer As New
System.Net.IPEndPoint(System.Net.IPAddress.Any, 0)
Dim MyByte() As Byte
Dim MyText As String

Do While Enabled
MyByte = GetSocket.Receive(RemoteComputer)
MyText = ASCII.GetString(MyByte)
RaiseEvent DataArrival(MyText, MyByte,
RemoteComputer.Address.ToString, RemoteComputer.Port)
Loop
End Sub

Your constructor looks like this:

' Set the enabled property to true.
Enabled = True

Private MyThread As Thread
MyThread = New Thread(AddressOf GetData)
MyThread.Start()

Then, to stop it, just set Enabled = false, and it should exit
gracefully (you should dispose of the UdpClient in the GetData method.

Also, you should have a Dispose method as well, so you can indicate when
the thread should be terminated, as putting the code in the finalizer
doesn't guarantee that the thread stops when it goes out of scope.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)


Peter Krikelis said:
Nicholas,

I did try that approach. If you bear with me here I will outline it here.
I
wrote a UDP socket wrapper. I have a GetData method that runs on a
separate
thread that raises an event as follows::
Public Enum SocketStates
NotBound = 0
Bound = 1
End Enum

Private MyState As SocketStates

Private Sub GetData()
Dim GetSocket As New UdpClient(iLocalPort)
Dim RemoteComputer As New
System.Net.IPEndPoint(System.Net.IPAddress.Any, 0)
Dim MyByte() As Byte
Dim MyText As String

Do While MyState = SocketStates.Bound

MyByte = GetSocket.Receive(RemoteComputer)
MyText = ASCII.GetString(MyByte)
RaiseEvent DataArrival(MyText, MyByte,
RemoteComputer.Address.ToString, RemoteComputer.Port)

Loop

End Sub

This method loops for ever and raises an event whenever data comes in.

I have a cleanup method that called StopSocket that is invoked before the
application terminates.
Public Sub StopSocket()

If MyState = SocketStates.Bound Then
MyState = SocketStates.NotBound
SendText("", "LocalHost", iLocalPort)
End If

End Sub

Now before the application that instantiated the class library exits it
calls the StopSocket() method.

My problem is that this works fine if the project using this class library
is a VB NET project. But with a C# project the thread never terminates
gracefully. Thats why I was trying to kill the thread as part of
StopSocket().

I posted this here because I am trying to get this library to work with a
C#
project.

Any thoughts on this will be appreciated. Thanks in advance.

Pete.

Nicholas Paldino said:
Peter,

I wouldn't use Abort to terminate the thread. Rather, I would use
something that would send a signal which would tell the thread to get out
of
the wait state that it is in, and then have it exit the thread without
having to bash it.

This would probably resolve the issue.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

message
Hi,

I have a VB NET class library. In the constructor of the library I
spawn
off
a thread that is listening for some data. When it gets data it raises
an
event.

Constructor has code that looks like the following ::

Private MyThread As Thread
MyThread = New Thread(AddressOf GetData)
MyThread.Start()

In the destructor I have the following code to get rid of the spawned
off
thread ::

If (MyThread.ThreadState = ThreadState.Running) Then
MyThread.Abort()
MyThread.Join()
End If

The problem is this:: For a VB NET project using this class library
everything works.
But for C# the thread that was spawned off is not destroyed. This is
the
only thing that does not seem to be working well with a C# project
using
this
class library.

Is there any difference in the way C# and VB NET handle threads?

Thanks in advance,

Pete.
 
Back
Top