WorkerRemoveBroacast(Broadcast) Method

Removes a periodic CAN message from the list of registered Broadcast objects.

Definition

Namespace: Peak.Can.Basic
Assembly: PCANBasic.NET (in PCANBasic.NET.dll) Version: 4.9.0
C#
public bool RemoveBroacast(
	ref Broadcast broadcast
)

Parameters

broadcast  Broadcast
Represents the Broadcast object to be removed.

Return Value

Boolean
true if the Broadcast object was removed; false otherwise.

Remarks

If a Broadcast object needs to be temporarily deactivated, it is not needed to delete it and create it again. Single Broadcast objects can be temporarily deactivated using the PauseBroadcast method and reactivated again using the ResumeBroadcast method. Using the property MessageBroadcasting is possible to activate/deactivate the Broadcast object processing.

Example

The following example shows how to remove a Broadcast object.

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 broadcast couldn't be configured");
}
else
{
    Console.WriteLine($"The broadcast object with index '{broadcast.Index}' was configured successfully.");

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

        // Wait sometime for the message to be sent several times
        //
        Console.WriteLine("Broadcast is working....");
        System.Threading.Thread.Sleep(5000);

        // Remove the broadcast using an object
        //
        if (myWorker.RemoveBroacast(ref broadcast))
        {
            Console.WriteLine("The broadcast object was successfully removed.");

            // Wait sometime to see the message is no being sent anymore
            //
            System.Threading.Thread.Sleep(3000);
        }
        else
            Console.WriteLine($"The broadcast with index {broadcast.Index} couldn't be removed.");

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

ArgumentNullExceptionThe broadcast parameter value is null. An object of type Broadcast is required.

See Also