I
I
INTERNALINTERFERENCE2022-01-18 17:31:59
C++ / C#
INTERNALINTERFERENCE, 2022-01-18 17:31:59

How to add elements to a collection once per second?

I have a collection:

public ViewModelCollection<ReceivedApplicationMessageViewModel> ReceivedApplicationMessages { get; } = new ();

Elements are constantly added to it because of what ui is overloaded.
I added another collection:
public ViewModelCollection<ReceivedApplicationMessageViewModel> ReceivedApplicationMessagesChunk { get; } = new();


The plan is this: constantly add elements to ReceivedApplicationMessagesChunk, but throw everything into ReceivedApplicationMessages every second and clear the chunk collection. How to implement all this?
Here's what I have so far:
public ViewModelCollection<ReceivedApplicationMessageViewModel> ReceivedApplicationMessages { get; } = new ();      
                                                                                                                    
public ViewModelCollection<ReceivedApplicationMessageViewModel> ReceivedApplicationMessagesChunk { get; } = new();  
public Task HandleApplicationMessageReceivedAsync( MqttApplicationMessageReceivedEventArgs eventArgs )              
{                                                                                                                   
    return Dispatcher.UIThread.InvokeAsync( () =>                                                                   
    {                                                                                                                                                  
        ChunkMessages( eventArgs.ApplicationMessage );                                                                                                                                                                                         
    } );                                                                                                             
}                                                                                                                   
                                                                                                                    
private void ChunkMessages( MqttApplicationMessage applicationMessage )                           
{                                                                                                 
    ReceivedApplicationMessagesChunk.Insert( 0, new ReceivedApplicationMessageViewModel(          
        _messageId++,                                                                             
        applicationMessage ) );                                                                   
                                                                                                  
    ReceivedApplicationMessages.AddRange( ReceivedApplicationMessagesChunk );                     
    ReceivedApplicationMessagesChunk.Clear();                                                     
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
O
oleg_ods, 2022-01-18
@oleg_ods

It is possible to try through the timer to implement.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question