I need to process 4000 message packets every second, from a total of 80 different IDs. I have a DataGrid
. I have ObservableCollection
binded to DataGrid. I tried 2 ways to refresh the UI without freezing. I was unsuccessful in both.
1.Way
If there is a message from a different ID, I add it to my collection. If it comes from the same ID, I delete the old one and get the new one.
if (MessagePacket._jobs.Where(X => X.ID == dataPacket.ID).FirstOrDefault() == null)
{
MessagePacket._jobs.Add(dataPacket);
}
else
{
for (int x = 0; x < MessagePacket._jobs.Count; x )
{
if (MessagePacket._jobs[x].ID == dataPacket.ID)
{
MessagePacket._jobs.RemoveAt(x);
MessagePacket._jobs.Add(dataPacket);
}
}
}
2.Way
I am modifying the data without deleting it and refreshing the collection.
Note : I tried to refresh the collection every 50ms, not after every pack. It's still slow.
if (MessagePacket._jobs.Where(X => X.ID == dataPacket.ID).FirstOrDefault() == null)
{
MessagePacket._jobs.Add(dataPacket);
}
else
{
for (int x = 0; x < MessagePacket._jobs.Count; x )
{
if (MessagePacket._jobs[x].ID == dataPacket.ID)
{
MessagePacket._jobs[x].DLC = dataPacket.DLC;
MessagePacket._jobs[x].RTR = dataPacket.RTR;
MessagePacket._jobs[x].IDE = dataPacket.IDE;
MessagePacket._jobs[x].Byte0 = dataPacket.Byte0;
MessagePacket._jobs[x].Byte1 = dataPacket.Byte1;
MessagePacket._jobs[x].Byte2 = dataPacket.Byte2;
MessagePacket._jobs[x].Byte3 = dataPacket.Byte3;
MessagePacket._jobs[x].Byte4 = dataPacket.Byte4;
MessagePacket._jobs[x].Byte5 = dataPacket.Byte5;
MessagePacket._jobs[x].Byte6 = dataPacket.Byte6;
MessagePacket._jobs[x].Byte7 = dataPacket.Byte7;
MessagePacket._jobs[x].Time = DateTime.Now.ToString("HH:mm:ss");
CollectionViewSource.GetDefaultView(MessagePacket._jobs).Refresh();
}
}
}
How can I speed up the up processes without freezing the ui. Or can you show an alternative way? Thank you from now.
CodePudding user response:
Something like this should be more efficient:
var p = MessagePacket._jobs.FirstOrDefault(x => x.ID == dataPacket.ID);
if (p == null)
{
MessagePacket._jobs.Add(dataPacket);
}
else
{
int i = MessagePacket._jobs.IndexOf(p);
MessagePacket._jobs[i] = dataPacket;
}
You may also consider to turn _jobs
into an ObservableDictionary, so that you could simply write
MessagePacket._jobs[dataPacket.ID] = dataPacket;