PowerSNMP for .NET
GetResponseTaskAsync Method
Example 




A GetMessage, GetNextMessage, GetBulkMessage, InformMessage or SetMessage.
The IPEndPoint of the target agent or manager.
Timeout, in milliseconds, to wait for the response. 3000 is a typical value. 0 should not be used.
Requests will be resent this many times if a response is not received within the Socket.ReceiveTimeout period.
Sends a request to the specified target and returns the response in an awaitable Task.
Syntax
Public Function GetResponseTaskAsync( _
   ByVal request As RequestMessage, _
   ByVal target As IPEndPoint, _
   ByVal timeout As Integer, _
   ByVal retries As Integer _
) As Task(Of ResponseMessage)
Dim instance As SnmpSocket
Dim request As RequestMessage
Dim target As IPEndPoint
Dim timeout As Integer
Dim retries As Integer
Dim value As Task(Of ResponseMessage)
 
value = instance.GetResponseTaskAsync(request, target, timeout, retries)

Parameters

request
A GetMessage, GetNextMessage, GetBulkMessage, InformMessage or SetMessage.
target
The IPEndPoint of the target agent or manager.
timeout
Timeout, in milliseconds, to wait for the response. 3000 is a typical value. 0 should not be used.
retries
Requests will be resent this many times if a response is not received within the Socket.ReceiveTimeout period.

Return Value

A Task that represents the operation. The value of the TResult parameter contains the ResponseMessage object encapsulating the response.
Remarks

If your application uses SnmpSocket.GetResponse() on parallel threads, consider using this method instead to execute those requests asynchronously. This technique avoids the overhead of multiple worker threads. A SocketException is thrown if a timeout condition causes the socket to be closed.

This method causes a byte array to be pinned while waiting for a response. If a response is not received, this byte array will be pinned until the timeout period expires. If an infinite timeout is specified, its receiving byte array will remain pinned. This method can be cancelled by closing the underlying Socket.

Example
The following example demonstrates sending an SNMPv1 Get request to an agent and receiving the response using await.
private async void button1_Click(object sender, EventArgs e)
{
    //Create Get Request
    GetMessage request = new GetMessage();
    request.Variables.Add(manager1.Mib.CreateVariable(NodeName.sysContact));

    //Create socket for sending request from. Binds to IPAddress.Any, port 0.
    SnmpSocket managerSocket = new SnmpSocket(manager1);

    //Send request and receive response
    ResponseMessage response = await managerSocket.GetResponseTaskAsync(request, myAgentAddress, 3000, 3);

    //Display info about the first variable in the response, and its value
    Variable vari = response.Variables[0];
    label1.Text += vari.Definition.ToString() + vari.Value.ToString() + Environment.NewLine;
}
Private Async Sub button1_Click(ByVal sender As Object, ByVal e As EventArgs)
    'Create Get Request
    Dim request As New GetMessage()
    request.Variables.Add(manager1.Mib.CreateVariable(NodeName.sysContact))

    'Create socket for sending request from. Binds to IPAddress.Any, port 0.
    Dim managerSocket As New SnmpSocket(manager1)

    'Send request and receive response
    Dim response As ResponseMessage = Await managerSocket.GetResponseTaskAsync(request, myAgentAddress, 3000, 3)

    'Display info about the first variable in the response, and its value
    Dim vari As Variable = response.Variables(0)
    label1.Text &= vari.Definition.ToString() & vari.Value.ToString() & Environment.NewLine
End Sub
See Also

Reference

SnmpSocket Class
SnmpSocket Members


PowerSNMP for .NET Documentation Version 8.0
© 2024 Dart Communications. All Rights Reserved.
Send comments on this topic