I have this thread protected override void Thread_Polling(string tokenStr, ScanParams parameters)
{
//Tache de polling des équipements
while (State != States.Stop)
{
//Traitement des commandes si nécéssaire
if (Commands.Count > 0 && Commands.TryDequeue(out Command command))
{
string token;
int equipmentID;
object config;
code
switch (command.CommandType)
{
case CommandTypeEnum.GetRealTimeData:
//Récupération des données real time d'un équipement
token = command.GetParameter("token") as string;
equipmentID = (int)command.GetParameter("equipmentID");
getRealTimeData(token, equipmentID);
break;
case CommandTypeEnum.GetConfigData:
//Récupération de la config
token = command.GetParameter("token") as string;
equipmentID = (int)command.GetParameter("equipmentID");
getConfigData(token, equipmentID);
break;
case CommandTypeEnum.GetHistory:
token = command.GetParameter("token") as string;
equipmentID = (int)command.GetParameter("equipmentID");
getHistoryData(token, equipmentID);
break;
case CommandTypeEnum.DeleteLogs:
token = command.GetParameter("token") as string;
equipmentID = (int)command.GetParameter("equipmentID");
deleteHistoryData(token, equipmentID);
break;
case CommandTypeEnum.SetConfigData:
token = command.GetParameter("token") as string;
equipmentID = (int)command.GetParameter("equipmentID");
config = command.GetParameter("config") as object;
setConfigData(token, equipmentID, config);
break;
case CommandTypeEnum.EraseZones:
token = command.GetParameter("token") as string;
equipmentID = (int)command.GetParameter("equipmentID");
config = command.GetParameter("config") as object;
deleteZonesData(token, equipmentID, config);
break;
}
}
else
{
if (foundEquipment != null)
{
//Polling des équipements (récupération des alarmes)
for (int i = 0; i < foundEquipment.Count; i++)
{
bool success = getAlarmData(i);
if (!success)
{
// Handle the case where getAlarmData failed for the current equipment
// For example, you can log the failure or take other appropriate action
}
else
{
}
}
}
}
Thread.Sleep(100);
}
} and an API controller public IActionResult DeleteEquipmentScanned([FromRoute] int comPort, [FromHeader] string Authorization)
{
try
{
bool ret = DataBaseManagement.DeleteComPortConfigByPortNumber(comPort);
code
if (ret)
{
return JSonResultExtension.Json2(HttpStatusCode.OK, new { result = true });
} else
{
return JSonResultExtension.Json2(HttpStatusCode.NotFound, new { result = false });
}
return JSonResultExtension.Json2(HttpStatusCode.BadRequest, new { msg = "Error deleting the com port data base", error = true });
}
} I want when I call this API to clear foundEquipmentlist without any conflict
Assistant
To clear the foundEquipment list when you call the API without causing any conflicts, you will need to ensure that simultaneous access to the list by multiple threads is handled safely. This typically involves using thread synchronization mechanisms such as locks.
Here's a general approach to how you could modify your DeleteEquipmentScanned API and the Thread_Polling method to safely clear the foundEquipment list:
Add a synchronization object to your class to lock on when accessing the foundEquipment list.
Modify the DeleteEquipmentScanned API to lock the synchronization object before clearing the list.
Update the Thread_Polling method to also lock on the same synchronization object when accessing the foundEquipment list.
Here's an example of how you might implement these changes:
csharp
// Add a private lock object for synchronization
private readonly object _equipmentLock = new object();
// Modify your API controller to clear the list safely
public IActionResult DeleteEquipmentScanned([FromRoute] int comPort, [FromHeader] string Authorization)
{
try
{
bool ret = DataBaseManagement.DeleteComPortConfigByPortNumber(comPort);
if (ret)
{
lock (_equipmentLock) // Ensure thread-safe access to the foundEquipment list
{
foundEquipment.Clear(); // Clear the list safely inside the lock
}
return JSonResultExtension.Json2(HttpStatusCode.OK, new { result = true });
}
else
{
return JSonResultExtension.Json2(HttpStatusCode.NotFound, new { result = false });
}
}
catch (Exception ex)
{
Console.WriteLine("GetEquipmentScanned exception : " + ex.Message);
return JSonResultExtension.Json2(HttpStatusCode.BadRequest, new { msg = "Error deleting the com port data base", error = true });
}
}
// Update your Thread_Polling method to use the lock when accessing foundEquipment
protected override void Thread_Polling(string tokenStr, ScanParams parameters)
{
// ... (rest of your method)
else
{
lock (_equipmentLock) // Ensure thread-safe access to the foundEquipment list
{
if (foundEquipment != null)
{
// ... (polling logic)
}
}
}
// ... (rest of your method)
}
By locking on _equipmentLock before accessing the foundEquipment list, you ensure that only one thread can modify the list at a time, preventing race conditions and conflicts.
Note: Make sure to use the lock wherever you access the foundEquipment list in your code to maintain thread safety. The lock should be held for as short a time as possible to avoid performance penalties due to thread contention.