Login  
Search All Forums
Dart Home | PowerTCP Winsock for ActiveX | Custom Development Reply | PowerTCP Winsock for ActiveX Topics | Forums   
AuthorForum: PowerTCP Winsock for ActiveX
Topic: DartPing and multi-threading in VB.NET
Symple

From: Copenhagen, Denmark
Posts: 3
Member Since: 06/22/04
posted June 22, 2004 10:48 AM

Hi Dart-team,

I'm developing an app to monitor whether several servers and other network enabled gadgets are online and available. This app should also gather some statistics (average responsetimes, number of timeouts, etc.). Pings should occur every 5 seconds or so. We are talking about somewhere in the line of 30-50 devices.

My question is how should I handle this? I've tried building a multi-threaded app, in which the Ping-object is embedded in a wrapper-class (2 classes, actually). This wrapper should then handle the actual ping by creating and using its own DartPing object by blocking in a separate thread, and then catch an event when that thread completes.

My problem is that if I instantiate 5 wrapper-class objects and let those objects ping different hosts using different threads, the results somehow seem to influence eachother (responsetimes can be e.g. 40ms, 260ms, 761ms, 1261ms, 1783ms - pinging the hosts separately will produce responses below 50ms consistently). Isn't the DartPing-object supposed to be thread-safe, so that one can create any number of Ping-objects and use them at the same time?

I've thought about using non-blocking pings instead, but how would that work when pinging 50 different hosts at 5 second intervals? As far as I can tell, you would use the ID property to keep track of the different pings, but I cannot figure out what will happen when the timeout is set to 0 seconds (in order to use non-blocking), the default timeout is 30 seconds and I want to ping every 5 secs - when will the ping-request actually time out, if the host does not respond?

If you could offer any thoughts on how to handle this problem I would be much obliged.


Best regards
\\Symple//
Alex Gladshtein



From: Rome, NY USA
Posts: 131
Member Since: 12/27/00
posted June 23, 2004 5:29 PM

Hello,

My first suggestion would be to use our .NET Sockets product with Ping in .NET. The ActiveX version is not thread-safe. It is marked for both apartment and free-threading, but that is primarily for COM+ users. For those in multi-threaded environments, you will have to handle your own thread-safety, which is why I recommend the .NET product. With that out of the way, your problem may be related more to thread management than safety. One quick test would be to try our .NET trial version and see if you have the same problem. That may be a quick test to narrow down whether the issue is your code.
Symple

From: Copenhagen, Denmark
Posts: 3
Member Since: 06/22/04
posted June 25, 2004 10:02 AM

Hi Alex,

First of all, thank you for replying to my post.

I've switched to using the .NET Sockets 1.0.2 component, and it solved my initial problem - the different threads no longer seem to influence eachother.

However, I now have another rather strange problem: Even if I set the timeout-value to e.g. 3000ms and do a blocking ping (still in a separate thread), the Sockets.Ping-object doesn't seem to return if the host does not respond. The next time I try to execute a ping using that object I get an exception. The exception.ToString tells me the following:

BusyException: Operation in progress.
  at q.b()
  at q.Invoke(Delegate A_0, Object[] A_1)
  at Dart.PowerTCP.Sockets.Ping.Send(String remoteHost, Int32 TTL, Int32 sequenceNumber, String localHost, Byte[] data)
  at Dart.PowerTCP.Sockets.Ping.Send(String remoteHost)
  at PingTool.Pinger.ActualPing() in C:\Visual Studio Projects\PingTool\Form1.vb:line 973


If I do a test with just 1 ping-object in the applications main thread, I get the correct response:
"A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond."

Do you have any idea why the component does not return an error instead of just locking up? Am I doing something wrong or could there be a bug somewhere?

I've copied in the code to my class if you feel like testing it out...


Public Class Pinger
  Public Event ThreadComplete(ByVal ResponseTime As Integer)
  Friend strHost As String
  Dim intResponseTime As Integer
  Dim PingerDotNet As New Dart.PowerTCP.Sockets.Ping
  Friend t As New Threading.Thread(AddressOf ActualPing)
  Sub New(ByVal Timeout As Integer, ByVal Host As String)
    MyBase.New()
    PingerDotNet.Timeout = Timeout
    strHost = Host
  End Sub

  Public Sub DoPing()
    t = New Threading.Thread(AddressOf ActualPing)
    t.Start()
  End Sub

  Public Sub ActualPing()
    Dim objEchoResult As Dart.PowerTCP.Sockets.EchoResult
    intResponseTime = -1
    System.Diagnostics.Debug.WriteLine(Now & " - " & strHost & ": Pinging...")
    Try
      objEchoResult = PingerDotNet.Send(strHost)
      System.Diagnostics.Debug.WriteLine(Now & " - " & strHost & ": Echo received")
      intResponseTime = objEchoResult.ResponseTime
      If intResponseTime = 0 Then intResponseTime = 10
    Catch ex As Exception
      System.Diagnostics.Debug.WriteLine(Now & "Error: " & strHost & ": " & ex.ToString)
    End Try

    RaiseEvent ThreadComplete(intResponseTime)
    Exit Sub
  End Sub
End Class


Thanks in advance!

Best regards
\\Symple//
Alex Gladshtein



From: Rome, NY USA
Posts: 131
Member Since: 12/27/00
posted June 25, 2004 12:43 PM

Hello,

The busy exception would occur if you are attempting to re-use the same instance of Ping while it is still attempting to complete an operation. This protects from re-entrancy problems. Here is my recommendation if I understand what you are trying to accomplish. If you use our product asynchronously (call BeginSend rather than Send), we will spawn a worker thread ourselves. If you need multiple Pings, just instantiate multiple Pings in a loop, use BeginSend, and in the EndSend event you can see who is returning by checking the Sender object. This will let you remove all that threading code and let us handle it.
Symple

From: Copenhagen, Denmark
Posts: 3
Member Since: 06/22/04
posted June 25, 2004 1:02 PM

Hi,

It does sound a lot simpler to let the component handle all the threading. However, I can't but wonder whether that will fix the problem with the component not returning properly.

I'll give it a go, and let you know.

Thanks a lot for your help so far.
Alex Gladshtein



From: Rome, NY USA
Posts: 131
Member Since: 12/27/00
posted June 25, 2004 1:10 PM

Hello,
If you continue having problems, create a small .NET project that we can run here, zip it and send it to support@dart.com with a reference to the topic: http://support.dart.com/reply?topicid=4420
We will run it and try to see what is happening under the hood. We usually prefer that to cut and paste as often times code is left out.

Regards,
Alex Gladshtein
Dart Communications
Reply | PowerTCP Winsock for ActiveX Topics | Forums   
This site is powered by PowerTCP WebServer Tool PowerTCP WebServer for ActiveX