public class Test
{
private readonly IConfiguration _config;
private readonly IHost _host;
private readonly Fake fakes;
private List<List<Message>> partitions = default!;
public Test()
{
fakes = new Fake();
this._config = new ConfigurationBuilder().AddJsonFile("appsettings.json").Build();
this._host = new Container(this._config.GetSection("Config")).HostBuilder.Build();
PrepareTest();
}
public void PrepareTest()
{
msg = fakes.messages(10);
//private List<List<Message>> partitions = default!;
partitions = messages.partition(1);
}
public void Send()
{
List<Thread> threads = new();
for(int i = 0; i < partitions.Count; i++)
{
int j = i;
new Thread(async () =>
{
Helper obj = new Helper(_host);
await obj.Post(partitions[j]);
}).Start();
}
}
}
public class Helper
{
private readonly IHost _host;
List<Message> a = new List<Message>();
public Helper(IHost host)
{
_host = host;
}
public async Task Post(List<Message> messages)
{
var _postService = _host.Services.GetService<IPostMessage>()!;
var response = _postService.PostMessage(messages);
await response;
//await Task.Delay(1000);
}
}
When I call it it works well when number of thread created is less than 10, but if thread count is increased to some value greater than 10, then the "index was outside the boundary" exception is thrown.
The exception is thrown at await obj.Post(partitions[j]);
ArgumentOutOfRangeException
Source array was not long enough. Check the source index, length, and the array's lower bounds. (Parameter 'sourceArray')
at System.Array.Copy(Array sourceArray, Int32 sourceIndex, Array destinationArray, Int32 destinationIndex, Int32 length, Boolean reliable)
at System.Array.Copy(Array sourceArray, Int32 sourceIndex, Array destinationArray, Int32 destinationIndex, Int32 length)
at System.Collections.Generic.Queue1.SetCapacity(Int32 capacity)
at System.Collections.Generic.Queue1.Enqueue(T item)
Could someone tell me how to fix this and why is this happening. Thanks!