C# Parallel foreach Parallel Source array was not long enough. Check srcIndex and length, Destination array was not long enough. Check destIndex and length, and the array's lower bounds

//Index was outside the bounds of the array.

//Source array was not long enough. Check srcIndex and length, and the array's lower bounds
//
using
System; using System.Collections.Concurrent; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; namespace WpfApp135 { /// <summary> /// Interaction logic for MainWindow.xaml /// </summary> public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); ConvertAAndB(); } void ConvertAAndB() { System.Diagnostics.Stopwatch watch = new System.Diagnostics.Stopwatch(); watch.Start(); List<AModel> modelAList = new List<AModel>(); for(int i=0;i<200000;i++) { modelAList.Add(new AModel { Id = i + 1, Name = $"Name_{i + 1}", Author = $"Author_{i + 1}", Title = $"Title_{i + 1}", ISBN = $"ISBN_{i + 1}", Topic = $"Topic_{i + 1}" }); } watch.Stop(); System.Diagnostics.Debug.WriteLine($"Generate :{modelAList.Count},milliseconds:{watch.ElapsedMilliseconds}"); List<BModel> modelBList = new List<BModel>(); watch.Restart(); //for(int i=0;i<200000;i++) //{ // modelBList.Add(new BModel // { // Id = modelAList[i].Id, // Name = modelAList[i].Name, // Author = modelAList[i].Author, // Title = modelAList[i].Title, // Topic = modelAList[i].Topic, // ISBN = modelAList[i].ISBN // }); //} Parallel.ForEach(modelAList, x => { modelBList.Add(new BModel { Id = x.Id, Name = x.Name, Author = x.Author, Title = x.Title, Topic = x.Topic, ISBN = x.ISBN }); }); watch.Stop(); System.Diagnostics.Debug.WriteLine($"Convert :{modelBList.Count},milliseconds:{watch.ElapsedMilliseconds}"); } } class AModel { public int Id { get; set; } public string Name { get; set; } public string Author { get; set; } public string Title { get; set; } public string ISBN { get; set; } public string Topic { get; set; } } class BModel { public int Id { get; set; } public string Name { get; set; } public string Author { get; set; } public string Title { get; set; } public string ISBN { get; set; } public string Topic { get; set; } } }

 

 

 

Plan A:

You're accessing the list by different threads, but a list is not threadsafe:

You could lock the list with:

lock(modelBList) 

{

}

Parallel.ForEach(modelAList, x =>
{
    lock(modelBList) 
    {
        modelBList.Add(new BModel
        {
            Id = x.Id,
            Name = x.Name,
            Author = x.Author,
            Title = x.Title,
            Topic = x.Topic,
            ISBN = x.ISBN
        });
    } 
});

 

Plan B:

ConcurrentBag

 ConcurrentBag<BModel> modelBList = new ConcurrentBag<BModel>();

void ConvertAAndB()
{
    System.Diagnostics.Stopwatch watch = new System.Diagnostics.Stopwatch();
    watch.Start();
    List<AModel> modelAList = new List<AModel>();
    for(int i=0;i<200000;i++)
    {
        modelAList.Add(new AModel
        {
            Id = i + 1,
            Name = $"Name_{i + 1}",
            Author = $"Author_{i + 1}",
            Title = $"Title_{i + 1}",
            ISBN = $"ISBN_{i + 1}",
            Topic = $"Topic_{i + 1}"
        });
    }
    watch.Stop();
    System.Diagnostics.Debug.WriteLine($"Generate :{modelAList.Count},milliseconds:{watch.ElapsedMilliseconds}");

    //List<BModel> modelBList = new List<BModel>();
    ConcurrentBag<BModel> modelBList = new ConcurrentBag<BModel>();
    watch.Restart();
    //for(int i=0;i<200000;i++)
    //{
    //    modelBList.Add(new BModel
    //    {
    //        Id = modelAList[i].Id,
    //        Name = modelAList[i].Name,
    //        Author = modelAList[i].Author,
    //        Title = modelAList[i].Title,
    //        Topic = modelAList[i].Topic,
    //        ISBN = modelAList[i].ISBN
    //    });
    //}
    Parallel.ForEach(modelAList, x =>
    {
        modelBList.Add(new BModel
        {
            Id = x.Id,
            Name = x.Name,
            Author = x.Author,
            Title = x.Title,
            Topic = x.Topic,
            ISBN = x.ISBN
        });
    });
    watch.Stop();
    System.Diagnostics.Debug.WriteLine($"Convert :{modelBList.Count},milliseconds:{watch.ElapsedMilliseconds}");

}

 

 

Copy from https://stackoverflow.com/questions/57785475/source-array-was-not-long-enough-check-srcindex-and-length-and-the-arrays-low

 

posted @ 2024-06-05 11:03  FredGrit  阅读(15)  评论(0编辑  收藏  举报