WorkerDequeue(PcanMessage, UInt64) Method

Removes and returns a CAN messages and its timestamp from the default receive queue (index 0).

Definition

Namespace: Peak.Can.Basic
Assembly: PCANBasic.NET (in PCANBasic.NET.dll) Version: 4.9.0
C#
public bool Dequeue(
	out PcanMessage message,
	out ulong timestamp
)

Parameters

message  PcanMessage
A PcanMessage object representing a received CAN message.
timestamp  UInt64
The timestamp of the message, in microseconds.

  Note

On Windows, the timestamp contains the number of microseconds elapsed since system start.

On Linux, the timestamp contains the number of microseconds elapsed since the epoch (00:00 UTC, January 1st 1970).

Return Value

Boolean
true if a message was successfully dequeued; otherwise, false.

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 a message from the default receive queue (index 0).

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.");

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

    PcanMessage message;
    ulong timestamp;
    if (myWorker.Dequeue(out message, out timestamp))
    {
        Console.WriteLine("New message retrieved. ");
        Console.WriteLine($"Timestamp: {timestamp} | {message}");
    }

    // 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}");
}

See Also