MatBlazor icon indicating copy to clipboard operation
MatBlazor copied to clipboard

MatToaster throws ArgumentOutOfRangeException

Open mihaimyh opened this issue 5 years ago • 8 comments

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]

mihaimyh avatar Jan 28 '20 11:01 mihaimyh

Can you send code example? Best way is https://blazorfiddle.com/

SamProf avatar Jan 28 '20 21:01 SamProf

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

sebestyn168 avatar Mar 16 '20 11:03 sebestyn168

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(....);

khew81 avatar Nov 18 '20 12:11 khew81

@mihaimyh , Are you still experiencing this issue?

Christian-Oleson avatar Dec 13 '20 21:12 Christian-Oleson

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.

sjahongir avatar Jan 16 '21 05:01 sjahongir

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).

rob-ack avatar Jun 17 '22 07:06 rob-ack

I have the same problem it's hard to reproduce the problem but it happens suddenly what's workaround about that?

yeganehaym avatar Jan 11 '23 11:01 yeganehaym

MatToaster.Add(....); await Task.Delay(100); MatToaster.Add(....); await Task.Delay(100);

sebestyn168 avatar Jan 11 '23 11:01 sebestyn168