WaitAny Exception (SafetyHandle closed)

A free API for the communication with control devices according to UDS (ISO 14229-1)
Post Reply
mhkong
Posts: 2
Joined: Fri 15. Mar 2024, 06:58

WaitAny Exception (SafetyHandle closed)

Post by mhkong » Thu 18. Jul 2024, 03:56

Dear Support Team,

We are using PCAN-UDS.dll to UDS message process and ISOTP message to concurrent communication.

It works well but sometimes "SafetyHandle closed" exception occur WaitAny function in waitHandle.cs when we release device.

I am guessing it related with device release process and our device release function is below

please advise us

Best Regards,
MH

Code: Select all

private static bool ReleaseUDSPCANDev()
{
    cantp_status rtnTP = cantp_status.PCANTP_STATUS_OK;

    // Close receive event
    if (IntPtr.Size == 4)
    {
        UInt32 iBuffer = 0;
        rtnTP = CanTpApi.SetValue_2016(deviceInfo.UDSDeviceHandle, cantp_parameter.PCANTP_PARAMETER_RECEIVE_EVENT, ref iBuffer, sizeof(UInt32));
    }
    else if (IntPtr.Size == 8)
    {
        Int64 buffer = 0;
        byte[] byteArray = BitConverter.GetBytes(buffer);
        rtnTP = CanTpApi.SetValue_2016(deviceInfo.UDSDeviceHandle, cantp_parameter.PCANTP_PARAMETER_RECEIVE_EVENT, byteArray, sizeof(UInt64));
    }

    if (rtnTP != cantp_status.PCANTP_STATUS_OK)
    {
        Log.LogStr("FAULT", "Error in set receive event.");
        return false;
    }

    RawCANThread_UDSChk = false;
    if (RawCANThread_UDS != null)
    {
        RawCANThread_UDS.Abort();
        RawCANThread_UDS.Join();
        RawCANThread_UDS = null;
        Log.LogStr("Release", "Raw CAN write thread in UDS is aborted.");
    }

    RawCANReadEventThreadChk_UDS = false;
    if (RawCANReadEventThread_UDS != null)
    {
        RawCANReadEventThread_UDS.Abort();
        RawCANReadEventThread_UDS.Join();
        RawCANReadEventThread_UDS = null;
        Log.LogStr("Release", "Raw CAN receive thread in UDS is aborted.");
    }

    recvEvent2RawCAN_UDS.Close();
    Log.LogStr("Release", "Close raw CAN receive event.");

    rawCANRecvQueue_UDS.Clear();

    uds_status rtn = UDSApi.Uninitialize_2013(deviceInfo.UDSDeviceHandle);
    if (rtn != uds_status.PUDS_STATUS_OK)
    {
        Log.LogStr("FAULT", "Error in PCAN device release.");
        return false;
    }
    deviceInfo.UDSDeviceOpened = false;
    return true;
}

M.Heidemann
Sales & Support
Sales & Support
Posts: 1083
Joined: Fri 20. Sep 2019, 13:31

Re: WaitAny Exception (SafetyHandle closed)

Post by M.Heidemann » Thu 18. Jul 2024, 10:54

Hello!

Please make sure to end your threads and uninitialize your events & channels afterwards, we suspect that your thread is waiting on events that have been just released prior.

Please see if this resolves the behavior you're seeing.

BR

Marvin
---
Marvin Heidemann
PEAK-Support Team

Post Reply