MatBlazor
MatBlazor copied to clipboard
MatToaster throws ArgumentOutOfRangeException
I get the following exception when using generating multiple toasts one after other:
System.ArgumentOutOfRangeException
HResult=0x80131502
Message=Index was out of range. Must be non-negative and less than the size of the collection. (Parameter 'index')
Source=System.Private.CoreLib
StackTrace:
at System.ThrowHelper.ThrowArgumentOutOfRange_IndexException()
at System.Collections.Generic.List`1.RemoveAt(Int32 index)
at System.Collections.Generic.List`1.Remove(T item)
at MatBlazor.MatToaster.Remove(MatToast toast)
at MatBlazor.MatToast.InvokeOnClose()
at MatBlazor.BaseMatToastItem.TimerElapsed()
at MatBlazor.MatToastTransitionTimer.TransitionCallback(Object state)
at System.Threading.ExecutionContext.RunFromThreadPoolDispatchLoop(Thread threadPoolThread, ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Threading.TimerQueueTimer.CallCallback(Boolean isThreadPool)
at System.Threading.TimerQueueTimer.Fire(Boolean isThreadPool)
at System.Threading.TimerQueueTimer.System.Threading.IThreadPoolWorkItem.Execute()
at System.Threading.ThreadPoolWorkQueue.Dispatch()
This exception was originally thrown at this call stack:
System.ThrowHelper.ThrowArgumentOutOfRange_IndexException()
MatBlazor.MatToaster.Remove(MatBlazor.MatToast)
MatBlazor.MatToast.InvokeOnClose()
MatBlazor.BaseMatToastItem.TimerElapsed()
MatBlazor.MatToastTransitionTimer.TransitionCallback(object)
System.Threading.ExecutionContext.RunFromThreadPoolDispatchLoop(System.Threading.Thread, System.Threading.ExecutionContext, System.Threading.ContextCallback, object)
System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
System.Threading.TimerQueueTimer.CallCallback(bool)
...
[Call Stack Truncated]
Can you send code example? Best way is https://blazorfiddle.com/
I had the same problem while opening multiple Toaster in sync with no delay. You need all your call to the toaster to be async and add a delay after each call TimeSpan.FromMilliseconds(100) is enought for me
It's an old thread but I'm experiencing the same issue as well. Is it not possible to add 2 MatToaster in continuous two line? For eg:
MatToaster.Add(....); MatToaster.Add(....);
@mihaimyh , Are you still experiencing this issue?
It's an old thread but I'm experiencing the same issue as well. Is it not possible to add 2 MatToaster in continuous two line? For eg:
MatToaster.Add(....); MatToaster.Add(....);
I have the same issue.
I bet thats the same thing.
warn: Microsoft.AspNetCore.Components.Server.Circuits.RemoteRenderer[100]
Unhandled exception rendering component: Index was out of range. Must be non-negative and less than the size of the collection. (Parameter 'index')
System.ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection. (Parameter 'index')
at System.Collections.Generic.List`1.get_Item(Int32 index)
at System.Linq.Enumerable.ListPartition`1.ToArray()
at System.Linq.Buffer`1..ctor(IEnumerable`1 source)
at System.Linq.Enumerable.ReverseIterator`1.MoveNext()
at MatBlazor.MatToastContainer.BuildRenderTree(RenderTreeBuilder __builder)
at Microsoft.AspNetCore.Components.Rendering.ComponentState.RenderIntoBatch(RenderBatchBuilder batchBuilder, RenderFragment renderFragment, Exception& renderFragmentException)
i had a brief look into the code and i think that
https://github.com/SamProf/MatBlazor/blob/bea4f9582b3c3ad06e491d0e5a5f2e9e93ce0ad1/src/MatBlazor/Services/Toast/MatToaster.cs#L11
should be a concurrent container (ConcurrentBag<T>
here).
You are kinda iterating and manipulating the container from different threads (some worker thread and blazor render thread).
I have the same problem it's hard to reproduce the problem but it happens suddenly what's workaround about that?
MatToaster.Add(....); await Task.Delay(100); MatToaster.Add(....); await Task.Delay(100);