Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ' ***********************************************************************
- ' Author : Elektro
- ' Modified : 16-September-2015
- ' ***********************************************************************
- ' <copyright file="NetworkUtil.vb" company="Elektro Studios">
- ' Copyright (c) Elektro Studios. All rights reserved.
- ' </copyright>
- ' ***********************************************************************
- #Region " Public Members Summary "
- #Region " SubClasses "
- ' NetworkUtil.NetworkTrafficMonitor
- ' NetworkUtil.ProcessTrafficMonitor
- #End Region
- #Region " Events "
- ' NetworkUtil.NetworkTrafficMonitor.TrafficChanged(Object, NetworkTrafficMonitor.TrafficChangedEventArgs)
- ' NetworkUtil.ProcessTrafficMonitor.TrafficChanged(Object, NetworkTrafficMonitor.TrafficChangedEventArgs)
- #End Region
- #Region " Enumerations "
- ' NetworkUtil.ProcessTrafficMonitor.UpdateBehavior As Integer
- #End Region
- #Region " Properties "
- ' NetworkUtil.NetworkInterfaceNames As IEnumerable(Of String)
- ' NetworkUtil.NetworkTrafficMonitor.InterfaceName As string
- ' NetworkUtil.NetworkTrafficMonitor.IsActive As Boolean
- ' NetworkUtil.NetworkTrafficMonitor.UpdateBehavior As NetworkTrafficMonitor.UpdateBehaviorEnum
- ' NetworkUtil.NetworkTrafficMonitor.UpdateInterval As Integer
- ' NetworkUtil.ProcessTrafficMonitor.ProcessId As Integer
- ' NetworkUtil.ProcessTrafficMonitor.IsActive As Boolean
- ' NetworkUtil.ProcessTrafficMonitor.UpdateBehavior As NetworkTrafficMonitor.UpdateBehaviorEnum
- ' NetworkUtil.ProcessTrafficMonitor.UpdateInterval As Integer
- #End Region
- #Region " Methods "
- ' NetworkUtil.NetworkTrafficMonitor.New(String)
- ' NetworkUtil.NetworkTrafficMonitor.Start
- ' NetworkUtil.NetworkTrafficMonitor.Stop
- ' NetworkUtil.NetworkTrafficMonitor.Dispose
- ' NetworkUtil.ProcessTrafficMonitor.New(Integer)
- ' NetworkUtil.ProcessTrafficMonitor.Start
- ' NetworkUtil.ProcessTrafficMonitor.Stop
- ' NetworkUtil.ProcessTrafficMonitor.Dispose
- #End Region
- #Region " Functions "
- ' NetworkUtil.NetworkTrafficMonitor.GetAvaliableInterfaceNames As IEnumerable(Of String)
- ' NetworkUtil.NetworkTrafficMonitor.GetBytesReceived As Single
- ' NetworkUtil.NetworkTrafficMonitor.GetBytesSent As Single
- ' NetworkUtil.ProcessTrafficMonitor.GetBytesReceived As Single
- ' NetworkUtil.ProcessTrafficMonitor.GetBytesSent As Single
- #End Region
- #End Region
- #Region " Option Statements "
- Option Strict On
- Option Explicit On
- Option Infer Off
- #End Region
- #Region " Imports "
- Imports System
- Imports System.Collections.Generic
- Imports System.ComponentModel
- Imports System.Diagnostics
- Imports System.Linq
- Imports System.Net.NetworkInformation
- Imports System.Windows.Forms
- #End Region
- Public NotInheritable Class NetworkUtil
- #Region " Poperties "
- ''' ----------------------------------------------------------------------------------------------------
- ''' <summary>
- ''' Gets the current network interface names.
- ''' </summary>
- ''' ----------------------------------------------------------------------------------------------------
- ''' <value>
- ''' An <see cref="IEnumerable(Of String)"/> that enumerates the network interface names.
- ''' </value>
- ''' ----------------------------------------------------------------------------------------------------
- Public Shared ReadOnly Property NetworkInterfaceNames As IEnumerable(Of String)
- <DebuggerStepThrough>
- <DebuggerHidden>
- Get
- Return From ni As NetworkInterface In NetworkInterface.GetAllNetworkInterfaces()
- Select ni.Description
- End Get
- End Property
- #End Region
- #Region " Network Traffic Monitor "
- ''' ----------------------------------------------------------------------------------------------------
- ''' <summary>
- ''' Monitorizes the traffic of a network interface.
- ''' </summary>
- ''' ----------------------------------------------------------------------------------------------------
- Public Class NetworkTrafficMonitor : Implements IDisposable
- #Region " Objects / Read-Only vars. "
- ''' ----------------------------------------------------------------------------------------------------
- ''' <summary>
- ''' The performance counter that monitors the bytes sent.
- ''' </summary>
- ''' ----------------------------------------------------------------------------------------------------
- Protected bytesSentPerformanceCounter As PerformanceCounter
- ''' ----------------------------------------------------------------------------------------------------
- ''' <summary>
- ''' The performance counter that monitors the bytes received.
- ''' </summary>
- ''' ----------------------------------------------------------------------------------------------------
- Protected bytesReceivedPerformanceCounter As PerformanceCounter
- ''' ----------------------------------------------------------------------------------------------------
- ''' <summary>
- ''' The name of the Performance Counters category.
- ''' </summary>
- ''' ----------------------------------------------------------------------------------------------------
- Protected ReadOnly categoryName As String = "Network Interface"
- ''' ----------------------------------------------------------------------------------------------------
- ''' <summary>
- ''' The name of the Performance Counter that retrieves the bytes sent.
- ''' </summary>
- ''' ----------------------------------------------------------------------------------------------------
- Protected ReadOnly counterNameBytesSent As String = "Bytes Sent/sec"
- ''' ----------------------------------------------------------------------------------------------------
- ''' <summary>
- ''' The name of the Performance Counter that retrieves the bytes received.
- ''' </summary>
- ''' ----------------------------------------------------------------------------------------------------
- Protected ReadOnly counterNameBytesReceived As String = "Bytes Received/sec"
- ''' ----------------------------------------------------------------------------------------------------
- ''' <summary>
- ''' Timer that manages the counters update interval.
- ''' </summary>
- ''' ----------------------------------------------------------------------------------------------------
- Protected WithEvents trafficUpdateTimer As Timer
- #End Region
- #Region " Properties "
- ''' ----------------------------------------------------------------------------------------------------
- ''' <summary>
- ''' Gets the name of the network interface that is being monitored.
- ''' </summary>
- ''' ----------------------------------------------------------------------------------------------------
- ''' <value>
- ''' The name of the network interface that is being monitored.
- ''' </value>
- ''' ----------------------------------------------------------------------------------------------------
- Public Overridable ReadOnly Property InterfaceName As String
- Get
- Return Me.interfaceNameB
- End Get
- End Property
- ''' ----------------------------------------------------------------------------------------------------
- ''' <summary>
- ''' ( Backing Field )
- ''' The name of the network interface that is being monitored.
- ''' </summary>
- ''' ----------------------------------------------------------------------------------------------------
- Protected ReadOnly interfaceNameB As String
- ''' ----------------------------------------------------------------------------------------------------
- ''' <summary>
- ''' Gets a value that indicates whether the traffic monitor is active.
- ''' </summary>
- ''' ----------------------------------------------------------------------------------------------------
- ''' <value>
- ''' A value that indicates whether the traffic monitor is active.
- ''' </value>
- ''' ----------------------------------------------------------------------------------------------------
- Public Overridable ReadOnly Property IsActive As Boolean
- Get
- Return Me.isActiveB
- End Get
- End Property
- ''' ----------------------------------------------------------------------------------------------------
- ''' <summary>
- ''' ( Backing Field )
- ''' A value that indicates whether the traffic monitor is active.
- ''' </summary>
- ''' ----------------------------------------------------------------------------------------------------
- Protected isActiveB As Boolean = False
- ''' ----------------------------------------------------------------------------------------------------
- ''' <summary>
- ''' Gets a value, in milliseconds, that determines when the monitor will update the traffic values.
- ''' </summary>
- ''' ----------------------------------------------------------------------------------------------------
- ''' <value>
- ''' A value, in milliseconds, that determines when the monitor will update the traffic values.
- ''' </value>
- ''' ----------------------------------------------------------------------------------------------------
- Public Overridable Property UpdateInterval As Integer
- Get
- Return Me.updateIntervalB
- End Get
- <DebuggerStepThrough>
- <DebuggerHidden>
- Set(ByVal value As Integer)
- Me.updateIntervalB = value
- Me.trafficUpdateTimer.Interval = value
- End Set
- End Property
- ''' ----------------------------------------------------------------------------------------------------
- ''' <summary>
- ''' ( Backing Field )
- ''' A value, in milliseconds, that determines when the monitor will update the traffic values.
- ''' </summary>
- ''' ----------------------------------------------------------------------------------------------------
- Protected updateIntervalB As Integer = 1000
- ''' ----------------------------------------------------------------------------------------------------
- ''' <summary>
- ''' Gets or sets a value that controls the behavior of the <see cref="TrafficChanged"/> event.
- ''' </summary>
- ''' ----------------------------------------------------------------------------------------------------
- ''' <value>
- ''' A value that controls the behavior of the <see cref="TrafficChanged"/> event.
- ''' </value>
- ''' ----------------------------------------------------------------------------------------------------
- Public Property UpdateBehavior As UpdateBehaviorEnum = UpdateBehaviorEnum.FireAlwaysAfterTick
- #End Region
- #Region " Enumerations "
- ''' <summary>
- ''' Specifies a behavior of the <see cref="TrafficChanged"/> event
- ''' </summary>
- Public Enum UpdateBehaviorEnum As Integer
- ''' <summary>
- ''' Fires the <see cref="TrafficChanged"/> event always when the specified <see cref="UpdateInterval"/> time ticks.
- ''' </summary>
- FireAlwaysAfterTick = 0I
- ''' <summary>
- ''' Fires the <see cref="TrafficChanged"/> event only if received or sent bytes changes,
- ''' after the the specified <see cref="UpdateInterval"/> time ticks.
- ''' </summary>
- FireWhenTrafficChangesAfterTick = 1I
- End Enum
- #End Region
- #Region " Events "
- ''' ----------------------------------------------------------------------------------------------------
- ''' <summary>
- ''' Occurs when network traffic changed.
- ''' </summary>
- ''' ----------------------------------------------------------------------------------------------------
- Protected Friend Event TrafficChanged As EventHandler(Of TrafficChangedEventArgs)
- #Region " Traffic Changd Args "
- ''' ----------------------------------------------------------------------------------------------------
- ''' <summary>
- ''' Contains the event-data of a <see cref="TrafficChanged"/> event.
- ''' </summary>
- ''' ----------------------------------------------------------------------------------------------------
- Protected Friend NotInheritable Class TrafficChangedEventArgs : Inherits EventArgs
- #Region " Properties "
- ''' ----------------------------------------------------------------------------------------------------
- ''' <summary>
- ''' Gets the bytes sent.
- ''' </summary>
- ''' ----------------------------------------------------------------------------------------------------
- ''' <value>
- ''' The bytes sent.
- ''' </value>
- ''' ----------------------------------------------------------------------------------------------------
- Public ReadOnly Property BytesSent As Single
- Get
- Return Me.bytesSentB
- End Get
- End Property
- ''' ----------------------------------------------------------------------------------------------------
- ''' <summary>
- ''' ( Backing Field )
- ''' Gets the bytes sent.
- ''' </summary>
- ''' ----------------------------------------------------------------------------------------------------
- Private ReadOnly bytesSentB As Single
- ''' ----------------------------------------------------------------------------------------------------
- ''' <summary>
- ''' Gets the bytes received.
- ''' </summary>
- ''' ----------------------------------------------------------------------------------------------------
- ''' <value>
- ''' The bytes received.
- ''' </value>
- ''' ----------------------------------------------------------------------------------------------------
- Public ReadOnly Property BytesReceived As Single
- Get
- Return Me.bytesReceivedB
- End Get
- End Property
- ''' ----------------------------------------------------------------------------------------------------
- ''' <summary>
- ''' ( Backing Field )
- ''' Gets the bytes received.
- ''' </summary>
- ''' ----------------------------------------------------------------------------------------------------
- Private ReadOnly bytesReceivedB As Single
- ''' ----------------------------------------------------------------------------------------------------
- ''' <summary>
- ''' Gets the difference between the last and the current amount of bytes sent.
- ''' For upload speed measuring purposes.
- ''' </summary>
- ''' ----------------------------------------------------------------------------------------------------
- ''' <value>
- ''' The difference between the last and the current amount of bytes sent.
- ''' </value>
- ''' ----------------------------------------------------------------------------------------------------
- Public ReadOnly Property DiffBytesSent As Single
- Get
- Return Me.diffBytesSentB
- End Get
- End Property
- ''' ----------------------------------------------------------------------------------------------------
- ''' <summary>
- ''' ( Backing Field )
- ''' The difference between the last and the current amount of bytes sent.
- ''' </summary>
- ''' ----------------------------------------------------------------------------------------------------
- Private ReadOnly diffBytesSentB As Single
- ''' ----------------------------------------------------------------------------------------------------
- ''' <summary>
- ''' Gets the difference between the last and the current amount of bytes received.
- ''' For download speed measuring purposes.
- ''' </summary>
- ''' ----------------------------------------------------------------------------------------------------
- ''' <value>
- ''' The difference between the last and the current amount of bytes received.
- ''' </value>
- ''' ----------------------------------------------------------------------------------------------------
- Public ReadOnly Property DiffBytesReceived As Single
- Get
- Return Me.diffBytesReceivedB
- End Get
- End Property
- ''' ----------------------------------------------------------------------------------------------------
- ''' <summary>
- ''' ( Backing Field )
- ''' The difference between the last and the current amount of bytes received.
- ''' </summary>
- ''' ----------------------------------------------------------------------------------------------------
- Private ReadOnly diffBytesReceivedB As Single
- #End Region
- #Region " Constructors "
- ''' ----------------------------------------------------------------------------------------------------
- ''' <summary>
- ''' Initializes a new instance of the <see cref="TrafficChangedEventArgs"/> class.
- ''' </summary>
- ''' ----------------------------------------------------------------------------------------------------
- ''' <param name="bytesSent">
- ''' The bytes sent.
- ''' </param>
- '''
- ''' <param name="bytesReceived">
- ''' The bytes received.
- ''' </param>
- '''
- ''' <param name="diffBytesSent">
- ''' The difference between the last and the current amount of bytes sent.
- ''' </param>
- '''
- ''' <param name="diffBytesReceived">
- ''' The difference between the last and the current amount of bytes received.
- ''' </param>
- ''' ----------------------------------------------------------------------------------------------------
- Public Sub New(ByVal bytesSent As Single,
- ByVal bytesReceived As Single,
- ByVal diffBytesSent As Single,
- ByVal diffBytesReceived As Single)
- Me.bytesSentB = bytesSent
- Me.bytesReceivedB = bytesReceived
- Me.diffBytesSentB = diffBytesSent
- Me.diffBytesReceivedB = DiffBytesReceived
- End Sub
- ''' <summary>
- ''' Prevents a default instance of the <see cref="TrafficChangedEventArgs"/> class from being created.
- ''' </summary>
- Private Sub New()
- End Sub
- #End Region
- End Class
- #End Region
- #End Region
- #Region " Constructors "
- ''' ----------------------------------------------------------------------------------------------------
- ''' <summary>
- ''' Initializes a new instance of the <see cref="NetworkTrafficMonitor"/> class.
- ''' </summary>
- ''' ----------------------------------------------------------------------------------------------------
- ''' <param name="interfaceName">
- ''' The name of the interface to monitor.
- ''' </param>
- ''' ----------------------------------------------------------------------------------------------------
- ''' <exception cref="NotImplementedException">
- ''' Performance Counters doesn't include the category.
- ''' </exception>
- '''
- ''' <exception cref="ArgumentException">
- ''' The specified interface is not avaliable or doesn't exist.;interfaceName
- ''' </exception>
- ''' ----------------------------------------------------------------------------------------------------
- <DebuggerStepThrough>
- <DebuggerHidden>
- Public Sub New(ByVal interfaceName As String)
- If Not Me.IsCategoryAvaliable(Me.categoryName) Then
- Throw New NotImplementedException(message:=String.Format("Performance Counters doesn't include the category '{0}'.",
- Me.categoryName))
- ElseIf Not Me.IsInterfaceAvaliable(interfaceName) Then
- Throw New ArgumentException(message:="The specified interface is not avaliable or doesn't exist.",
- paramname:="interfacename")
- Else
- Me.interfaceNameB = interfaceName
- Me.trafficUpdateTimer = New Timer
- End If
- End Sub
- ''' ----------------------------------------------------------------------------------------------------
- ''' <summary>
- ''' Initializes a new instance of the <see cref="NetworkTrafficMonitor"/> class.
- ''' </summary>
- ''' ----------------------------------------------------------------------------------------------------
- ''' <param name="categoryName">
- ''' The name of the performance category.
- ''' </param>
- '''
- ''' <param name="counterNameBytesSent">
- ''' The name of the Performance Counter that retrieves the bytes sent.
- ''' </param>
- '''
- ''' <param name="counterNameBytesReceived">
- ''' The name of the Performance Counter that retrieves the bytes received.
- ''' </param>
- ''' ----------------------------------------------------------------------------------------------------
- ''' <exception cref="NotImplementedException">
- ''' Performance Counters doesn't include the category.
- ''' </exception>
- ''' ----------------------------------------------------------------------------------------------------
- <DebuggerStepThrough>
- <DebuggerHidden>
- Protected Sub New(ByVal categoryName As String,
- ByVal counterNameBytesSent As String,
- ByVal counterNameBytesReceived As String)
- If Not Me.IsCategoryAvaliable(categoryName) Then
- Throw New NotImplementedException(message:=String.Format("Performance Counters doesn't include the category '{0}'.",
- categoryName))
- End If
- Me.categoryName = categoryName
- Me.counterNameBytesSent = counterNameBytesSent
- Me.counterNameBytesReceived = counterNameBytesReceived
- Me.trafficUpdateTimer = New Timer
- End Sub
- ''' ----------------------------------------------------------------------------------------------------
- ''' <summary>
- ''' Prevents a default instance of the <see cref="NetworkTrafficMonitor"/> class from being created.
- ''' </summary>
- ''' ----------------------------------------------------------------------------------------------------
- Private Sub New()
- End Sub
- #End Region
- #Region " Shared Methods "
- ''' ----------------------------------------------------------------------------------------------------
- ''' <summary>
- ''' Gets the avaliable network interface names.
- ''' </summary>
- ''' ----------------------------------------------------------------------------------------------------
- ''' <returns>
- ''' An <see cref="IEnumerable(Of String)"/> that enumerates the avaliable network interface names.
- ''' </returns>
- ''' ----------------------------------------------------------------------------------------------------
- <DebuggerStepThrough>
- <DebuggerHidden>
- Public Shared Function GetAvaliableInterfaceNames() As IEnumerable(Of String)
- If PerformanceCounterCategory.Exists("Network Interface") Then
- Return New PerformanceCounterCategory("Network Interface").GetInstanceNames
- Else
- Return Nothing
- End If
- End Function
- #End Region
- #Region " Public Methods "
- ''' ----------------------------------------------------------------------------------------------------
- ''' <summary>
- ''' Initializes the traffic monitoring.
- ''' </summary>
- ''' ----------------------------------------------------------------------------------------------------
- <DebuggerStepThrough>
- <DebuggerHidden>
- Public Overridable Sub Start()
- Me.InitializeMonitor()
- End Sub
- ''' ----------------------------------------------------------------------------------------------------
- ''' <summary>
- ''' Finalizes the traffic monitoring.
- ''' </summary>
- ''' ----------------------------------------------------------------------------------------------------
- <DebuggerStepThrough>
- <DebuggerHidden>
- Public Overridable Sub [Stop]()
- Me.isActiveB = False
- If Me.trafficUpdateTimer IsNot Nothing Then
- Me.trafficUpdateTimer.Enabled = False
- End If
- End Sub
- ''' ----------------------------------------------------------------------------------------------------
- ''' <summary>
- ''' Gets the bytes sent.
- ''' </summary>
- ''' ----------------------------------------------------------------------------------------------------
- ''' <returns>
- ''' The bytes sent.
- ''' </returns>
- ''' ----------------------------------------------------------------------------------------------------
- <DebuggerStepThrough>
- <DebuggerHidden>
- Public Overridable Function GetBytesSent() As Single
- If Me.isActiveB Then
- Return Me.bytesSentPerformanceCounter.RawValue
- Else
- Return Nothing
- End If
- End Function
- ''' ----------------------------------------------------------------------------------------------------
- ''' <summary>
- ''' Gets the bytes received.
- ''' </summary>
- ''' ----------------------------------------------------------------------------------------------------
- ''' <returns>
- ''' The bytes received.
- ''' </returns>
- ''' ----------------------------------------------------------------------------------------------------
- <DebuggerStepThrough>
- <DebuggerHidden>
- Public Overridable Function GetBytesReceived() As Single
- If Me.isActiveB Then
- Return Me.bytesReceivedPerformanceCounter.RawValue
- Else
- Return Nothing
- End If
- End Function
- #End Region
- #Region " Private Methods "
- ''' ----------------------------------------------------------------------------------------------------
- ''' <summary>
- ''' Determines whether the specified interface is avaliable.
- ''' </summary>
- ''' ----------------------------------------------------------------------------------------------------
- ''' <param name="interfaceName">
- ''' The name of the interface.
- ''' </param>
- ''' ----------------------------------------------------------------------------------------------------
- ''' <returns>
- ''' <see langword="True"/> if interface is avaliable; otherwise, <see langword="False"/>.
- ''' </returns>
- ''' ----------------------------------------------------------------------------------------------------
- <DebuggerStepThrough>
- <DebuggerHidden>
- Protected Function IsInterfaceAvaliable(ByVal interfaceName As String) As Boolean
- Return (From name As String In NetworkTrafficMonitor.GetAvaliableInterfaceNames
- Where name.Equals(interfaceName, StringComparison.OrdinalIgnoreCase)).Any
- End Function
- ''' ----------------------------------------------------------------------------------------------------
- ''' <summary>
- ''' Determines whether the specified performance counter category is avaliable.
- ''' </summary>
- ''' ----------------------------------------------------------------------------------------------------
- ''' <param name="categoryName">
- ''' The name of the category.
- ''' </param>
- ''' ----------------------------------------------------------------------------------------------------
- ''' <returns>
- ''' <see langword="True"/> if category is avaliable; otherwise, <see langword="False"/>.
- ''' </returns>
- ''' ----------------------------------------------------------------------------------------------------
- <DebuggerStepThrough>
- <DebuggerHidden>
- Protected Function IsCategoryAvaliable(ByVal categoryName As String) As Boolean
- Return PerformanceCounterCategory.Exists(categoryName)
- End Function
- ''' ----------------------------------------------------------------------------------------------------
- ''' <summary>
- ''' Gets the avaliable instance names of the specified performance counter category.
- ''' </summary>
- ''' ----------------------------------------------------------------------------------------------------
- ''' <returns>
- ''' An <see cref="IEnumerable(Of String)"/> that enumerates the avaliable instance names.
- ''' </returns>
- ''' ----------------------------------------------------------------------------------------------------
- <DebuggerStepThrough>
- <DebuggerHidden>
- Protected Overridable Function GetAvaliableInstanceNames(ByVal categoryName As String) As IEnumerable(Of String)
- If Me.IsCategoryAvaliable(categoryName) Then
- Return New PerformanceCounterCategory(categoryName).GetInstanceNames
- Else
- Return Nothing
- End If
- End Function
- ''' ----------------------------------------------------------------------------------------------------
- ''' <summary>
- ''' Initializes the performance counters.
- ''' </summary>
- ''' ----------------------------------------------------------------------------------------------------
- <DebuggerStepThrough>
- <DebuggerHidden>
- Protected Overridable Sub InitializeMonitor()
- If Not Me.isActiveB Then
- If Me.IsInterfaceAvaliable(Me.interfaceNameB) Then
- Me.bytesSentPerformanceCounter = New PerformanceCounter
- With bytesSentPerformanceCounter
- .CategoryName = Me.categoryName
- .CounterName = Me.counterNameBytesSent
- .InstanceName = Me.interfaceNameB
- .[ReadOnly] = True
- End With
- Me.bytesReceivedPerformanceCounter = New PerformanceCounter
- With bytesReceivedPerformanceCounter
- .CategoryName = Me.categoryName
- .CounterName = Me.counterNameBytesReceived
- .InstanceName = Me.interfaceNameB
- .[ReadOnly] = True
- End With
- Me.isActiveB = True
- Me.trafficUpdateTimer.Enabled = True
- End If
- End If
- End Sub
- ''' ----------------------------------------------------------------------------------------------------
- ''' <summary>
- ''' Finalizes the performance counters.
- ''' </summary>
- ''' ----------------------------------------------------------------------------------------------------
- <DebuggerStepThrough>
- <DebuggerHidden>
- Protected Overridable Sub FinalizeMonitor()
- Me.isActiveB = False
- If Me.trafficUpdateTimer IsNot Nothing Then
- Me.trafficUpdateTimer.Enabled = False
- Me.trafficUpdateTimer.Dispose()
- End If
- If Me.bytesSentPerformanceCounter IsNot Nothing Then
- Me.bytesSentPerformanceCounter.RemoveInstance()
- Me.bytesSentPerformanceCounter.Close()
- End If
- If Me.bytesReceivedPerformanceCounter IsNot Nothing Then
- Me.bytesReceivedPerformanceCounter.RemoveInstance()
- Me.bytesReceivedPerformanceCounter.Close()
- End If
- End Sub
- #End Region
- #Region " Event-Handlers "
- ''' ----------------------------------------------------------------------------------------------------
- ''' <summary>
- ''' Handles the <see cref="Timer.Tick"/> event of the <see cref="TrafficUpdateTimer"/> control.
- ''' </summary>
- ''' ----------------------------------------------------------------------------------------------------
- ''' <param name="sender">
- ''' The source of the event.
- ''' </param>
- ''' ----------------------------------------------------------------------------------------------------
- ''' <param name="e">
- ''' The <see cref="EventArgs"/> instance containing the event data.
- ''' </param>
- ''' ----------------------------------------------------------------------------------------------------
- <DebuggerStepThrough>
- <DebuggerHidden>
- Protected Overridable Sub TrafficUpdateTimer_Tick(ByVal sender As Object, ByVal e As EventArgs) _
- Handles trafficUpdateTimer.Tick
- If (TrafficChangedEvent IsNot Nothing) Then
- Static isFirstUpdate As Boolean = True
- Static lastBytesSent As Single
- Static lastBytesReceived As Single
- Dim currentBytesSent As Single = Me.GetBytesSent
- Dim currentBytesReceived As Single = Me.GetBytesReceived
- Dim diffBytessSent As Single = If(isFirstUpdate, 0, (currentBytesSent - lastBytesSent))
- Dim diffBytesReceived As Single = If(isFirstUpdate, 0, (currentBytesReceived - lastBytesReceived))
- Dim needUpdate As Boolean = False
- If (currentBytesSent <> lastBytesSent) OrElse (currentBytesReceived <> lastBytesReceived) Then
- needUpdate = True
- isFirstUpdate = False
- End If
- lastBytesSent = currentBytesSent
- lastBytesReceived = currentBytesReceived
- If (Me.UpdateBehavior = UpdateBehaviorEnum.FireAlwaysAfterTick) OrElse
- (needUpdate AndAlso Me.UpdateBehavior = UpdateBehaviorEnum.FireWhenTrafficChangesAfterTick) Then
- RaiseEvent TrafficChanged(Me, New TrafficChangedEventArgs(currentBytesSent, currentBytesReceived,
- diffBytessSent, diffBytesReceived))
- End If
- End If
- End Sub
- #End Region
- #Region " IDisposable Implementation "
- ''' ----------------------------------------------------------------------------------------------------
- ''' <summary>
- ''' To detect redundant calls when disposing.
- ''' </summary>
- ''' ----------------------------------------------------------------------------------------------------
- Protected isDisposed As Boolean = False
- ''' ----------------------------------------------------------------------------------------------------
- ''' <summary>
- ''' Releases all resources used by this instance.
- ''' </summary>
- ''' ----------------------------------------------------------------------------------------------------
- ''' <remarks>
- ''' Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
- ''' </remarks>
- ''' ----------------------------------------------------------------------------------------------------
- <DebuggerStepThrough>
- Public Sub Dispose() Implements IDisposable.Dispose
- Me.Dispose(isDisposing:=True)
- GC.SuppressFinalize(obj:=Me)
- End Sub
- ''' ----------------------------------------------------------------------------------------------------
- ''' <summary>
- ''' Releases unmanaged and - optionally - managed resources.
- ''' </summary>
- ''' ----------------------------------------------------------------------------------------------------
- ''' <param name="isDisposing">
- ''' <c>True</c> to release both managed and unmanaged resources;
- ''' <c>False</c> to release only unmanaged resources.
- ''' </param>
- ''' ----------------------------------------------------------------------------------------------------
- <DebuggerStepThrough>
- Protected Overridable Sub Dispose(ByVal isDisposing As Boolean)
- If (Not Me.isDisposed) AndAlso (isDisposing) Then
- Me.FinalizeMonitor()
- End If
- Me.isDisposed = True
- End Sub
- #End Region
- End Class
- #End Region
- #Region " Process Traffic Monitor "
- ''' ----------------------------------------------------------------------------------------------------
- ''' <summary>
- ''' Monitorizes the network traffic of a process.
- ''' </summary>
- ''' ----------------------------------------------------------------------------------------------------
- Public Class ProcessTrafficMonitor : Inherits NetworkTrafficMonitor
- #Region " Hidden Base Members "
- <EditorBrowsable(EditorBrowsableState.Never)>
- Friend Shadows InterfaceName As Object
- <EditorBrowsable(EditorBrowsableState.Never)>
- Private Shadows getAvaliableInterfaceNames As Object
- #End Region
- #Region " Properties "
- ''' ----------------------------------------------------------------------------------------------------
- ''' <summary>
- ''' Gets the name of the process that is being monitored.
- ''' </summary>
- ''' ----------------------------------------------------------------------------------------------------
- ''' <value>
- ''' The name of the process that is being monitored.
- ''' </value>
- ''' ----------------------------------------------------------------------------------------------------
- Public Overridable ReadOnly Property ProcessId As Integer
- Get
- Return Me.processIdB
- End Get
- End Property
- ''' ----------------------------------------------------------------------------------------------------
- ''' <summary>
- ''' ( Backing Field )
- ''' The name of the process that is being monitored.
- ''' </summary>
- ''' ----------------------------------------------------------------------------------------------------
- Protected ReadOnly processIdB As Integer
- #End Region
- #Region " Constructors "
- ''' ----------------------------------------------------------------------------------------------------
- ''' <summary>
- ''' Initializes a new instance of the <see cref="ProcessTrafficMonitor"/> class.
- ''' </summary>
- ''' ----------------------------------------------------------------------------------------------------
- ''' <param name="processId">
- ''' The PID of the process to monitor.
- ''' Note that the process should have the performance counters feature enabled to be able to use the counters.
- ''' </param>
- ''' ----------------------------------------------------------------------------------------------------
- ''' <remarks>
- ''' The application's configuration file (app.config) should look like this:
- '''
- ''' <code>
- ''' <![CDATA[<?xml version="1.0" encoding="utf-8"?>
- ''' <configuration>
- ''' ...
- ''' <system.net>
- ''' <settings>
- ''' <performanceCounters enabled="true"/>
- ''' </settings>
- ''' </system.net>
- ''' ...
- ''' </configuration>]]>
- ''' </code>
- '''
- ''' MSDN Documentation: <see href="http://msdn.microsoft.com/en-us/library/ms229151%28v=vs.110%29.aspx"/>
- ''' </remarks>
- ''' ----------------------------------------------------------------------------------------------------
- <DebuggerStepThrough>
- <DebuggerHidden>
- Public Sub New(ByVal processId As Integer)
- MyBase.New(categoryName:=".NET CLR Networking 4.0.0.0",
- counterNameBytesSent:="Bytes Sent",
- counterNameBytesReceived:="Bytes Received")
- Me.processIdB = processId
- End Sub
- #End Region
- #Region " Private Methods "
- ''' ----------------------------------------------------------------------------------------------------
- ''' <summary>
- ''' Initializes the performance counters.
- ''' </summary>
- ''' ----------------------------------------------------------------------------------------------------
- <DebuggerStepThrough>
- <DebuggerHidden>
- Protected Overrides Sub InitializeMonitor()
- If Not MyBase.isActiveB Then
- Dim instanceNames As IEnumerable(Of String) =
- From name As String In MyBase.GetAvaliableInstanceNames(MyBase.categoryName)
- Where name.Contains(String.Format("p{0}", Me.processIdB))
- If instanceNames.Any() Then
- MyBase.bytesSentPerformanceCounter = New PerformanceCounter
- With MyBase.bytesSentPerformanceCounter
- .CategoryName = MyBase.categoryName
- .CounterName = MyBase.counterNameBytesSent
- .InstanceName = instanceNames.First
- .[ReadOnly] = True
- End With
- MyBase.bytesReceivedPerformanceCounter = New PerformanceCounter
- With MyBase.bytesReceivedPerformanceCounter
- .CategoryName = MyBase.categoryName
- .CounterName = MyBase.counterNameBytesReceived
- .InstanceName = instanceNames.First
- .[ReadOnly] = True
- End With
- MyBase.isActiveB = True
- MyBase.trafficUpdateTimer.Enabled = True
- End If
- End If
- End Sub
- #End Region
- End Class
- #End Region
- End Class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement