WorkerClearBroadcasts Method

Removes all entries from the Worker object's broadcast list.

Definition

Namespace: Peak.Can.Basic
Assembly: PCANBasic.NET (in PCANBasic.NET.dll) Version: 4.9.0
C#
public void ClearBroadcasts()

Example

The following example shows how to clear the Worker object's broadcast list.

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();

// Create a Broadcast object using a CAN message and its interval
//
PcanMessage message = new PcanMessage(0x100, MessageType.Standard, 3, new byte[] { 1, 2, 3 }, false);
Broadcast broadcast = new Broadcast(message, 100);

// Add the new broadcast using a Broadcast object
//
if (!myWorker.AddBroadcast(ref broadcast))
{
    Console.WriteLine("The first broadcast couldn't be configured.");
}
else
{
    Console.WriteLine($"The broadcast object with index '{broadcast.Index}' was configured successfully.");

    // add a second broadcast
    //
    PcanMessage message2 = new PcanMessage(0x200, MessageType.Standard, 8, new byte[] { 8, 7, 6, 5, 4, 3, 2, 1 }, false);
    Broadcast broadcast2 = new Broadcast(message2, 500);
    if (!myWorker.AddBroadcast(ref broadcast2))
    {
        Console.WriteLine("The second broadcast couldn't be configured.");
    }
    else
    {
        Console.WriteLine($"The broadcast object with index '{broadcast2.Index}' was configured successfully.");

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

            // Wait sometime for the messages to be sent several times
            //
            Console.WriteLine("Broadcasting ....");
            System.Threading.Thread.Sleep(5000);

            // Clear all broadcasts 
            //
            myWorker.ClearBroadcasts();                        
            Console.WriteLine("All broadcast objects were removed.");

            // Wait sometime to validate that the message is not sent anymore
            //                        
            System.Threading.Thread.Sleep(3000);

            // 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