I
I
INTERNALINTERFERENCE2022-01-19 16:32:18
C++ / C#
INTERNALINTERFERENCE, 2022-01-19 16:32:18

How to implement multi-threaded sending of data to a collection?

I have a collection that constantly receives data. Then the data is sent once a second to another collection, and the original one is cleared. Sometimes an error appears: destination array was not long enough. check destindex and length and the array's lower bounds . As I understand it, this is due to the simultaneous modification of threads, but I do not know how to fix it.
Here is my code:

public SubscriptionsPageViewModel( )                                    
{                                                                                                                                                                                                                                                                          
    var timer = new Timer( 1000 ) {AutoReset = false};                                                      
    timer.Elapsed += ( sender, args ) => ChunkMessages();                                                   
    NewMessageArrived += (sender, args) => { if (!timer.Enabled) timer.Start(); };                          
}                                                                                                           
public static event EventHandler? NewMessageArrived;
public ViewModelCollection<ReceivedApplicationMessageViewModel> ReceivedApplicationMessages { get; } = new ();    
public ViewModelCollection<ReceivedApplicationMessageViewModel> ReceivedApplicationMessagesChunk { get; } = new();
                                                                                                                  
public Task HandleApplicationMessageReceivedAsync( MqttApplicationMessageReceivedEventArgs eventArgs )            
{                                                                                                                 
    return Dispatcher.UIThread.InvokeAsync( () =>                                                                 
    {                                                                                                             
        if ( ReceivedApplicationMessages.Count >= 10000 )                                                         
        {                                                                                                         
             ReceivedApplicationMessages.Clear();                                                                 
             _messageId = 0;                                                                                      
        }                                                                                                         
                                                                                                                  
        ReceivedApplicationMessagesChunk.Insert( 0, new ReceivedApplicationMessageViewModel(                      
            _messageId++,                                                                                         
            eventArgs.ApplicationMessage ) );                                                                     
                                                                                                                  
        NewMessageArrived?.Invoke(null, EventArgs.Empty);                                                         
    } );                                                                                                           
}                                                                                                                 
                                                                                                                  
private void ChunkMessages()                                                                                      
{                                                                                                                 
    ReceivedApplicationMessages.AddOrInsertRange( ReceivedApplicationMessagesChunk, 0 );                          
    ReceivedApplicationMessagesChunk.Clear();                                                                     
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
F
freeExec, 2022-01-19
@freeExec

https://docs.microsoft.com/en-us/dotnet/standard/c...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question