WorkerGetAvailableMessagesCount(Int32) Method

Retrieves the amount of available messages currently contained in the specified receive queue.

Definition

Namespace: Peak.Can.Basic
Assembly: PCANBasic.NET (in PCANBasic.NET.dll) Version: 4.9.0
C#
public int GetAvailableMessagesCount(
	int queueIndex
)

Parameters

queueIndex  Int32
Zero-based index that identifies the receive queue from which the amount of available messages should be retrieved.

Return Value

Int32
The amount of messages currently contained in the specified receive queue.

Remarks

Unlike the Api class, the Worker class doesn't clear the receive queues on disconnect. A Worker object muss explicitly be instructed to clear its receive queues when deactivating the connection, i.e. by calling the Stop method, or by calling the ClearReceiveQueue/ClearAllReceiveQueues methods at any time.

This implies, that an application can keep processing messages after a connection to a PCAN Channel has been terminated.

Example

The following example shows how to retrieve the amount of messages contained in the second receive queue (index 1).

In case of failure, an error messages is written to the console output using English as output language.

C#
// Create the object using the default configuration
//
Worker myWorker = new Worker();

try
{ 
    // Activate the worker object
    //
    myWorker.Start();
    Console.WriteLine("The Worker object was activated successfully.");

    // Set the second filter (index 1) to pass all messages 
    //
    myWorker.SetFilteringState(1, FilteringState.PassForAll);

    // Wait for sometime to get messages in the receive queue
    //
    Console.WriteLine("Waiting for messages......");
    System.Threading.Thread.Sleep(5000);

    // Get and show the amount of messages contained in the second receive queue (index 1)
    //
    Console.WriteLine($"There are {myWorker.GetAvailableMessagesCount(1)} messages in the second receive queue. ");

    // Deactivate the worker object, when it is no longer needed
    //
    myWorker.Stop();
    Console.WriteLine("The Worker object was deactivated.");
}
catch(Exception ex)
{
    Console.WriteLine($"An exception occurred by activating the Worker object. {ex.Message}");
}

Exceptions

IndexOutOfRangeExceptionThe queueIndex parameter value is out of range. This must be in the range [0...ReceiveQueuesCount-1].

See Also