亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

List<T> 中的 C# Parallel.Foreach int[] 未正確更改

List<T> 中的 C# Parallel.Foreach int[] 未正確更改

C#
皈依舞 2022-01-09 14:48:41
這是我的課:public class UnusedRolesOccurrence{    public string Username { get; set; }    public string FullName { get; set; }    public string Role { get; set; }    public int[] Month { get { return month; } set { month = value; } }    private static int[] month = new int[12];}這就是我想用值填充 int 數組的地方:Parallel.ForEach(unusedrolesoccurrence, (UnusedRolesOccurrence item) =>{    try    {        lock (listLock)        {            int count = tmp.Count(x => x.Username == item.Username && x.Role == item.Role);            item.Month[month] = count;            if (count > 2)            {                Debug.WriteLine($"{item.Username},{item.Role},{month}={count},{item.Month[month]}");            }        }    }    catch    {        //    }});List<unusedrolesoccurrence>預先填充了數據。在開始 foreach 之前,月份數組是 [0,0,0,0,0,0,0,0,0,0,0,0],使用int[] arr_month = new int[12];.tmp也是一個List<t>。什么不工作:在循環期間,計數和值item.Month[month]也是正確的。但不在目標中List<UnusedRolesOccurrence>。所有月份都是循環中處理的最后一個計數的相同值,例如 [3,0,3,0,0,0,0,0,0,0,0,0] 或 [0,1,1,0 ,0,0,0,0,0,0,0,0]。而且因為并行,結果當然總是不同的。我換int[]到public Dictionary<int, int> Month { get; set; }測試,但同樣的行為。這里的示例代碼(使用 // 二次嘗試使用具有相同結果的字典):public class UnusedRoles        {            public string Username { get; set; }            public string Role { get; set; }            public int Month { get; set; }        }        public class UnusedRolesOccurrence        {            public string Username { get; set; }            public string Role { get; set; }            //public Dictionary<int, int> Month { get; set; }            public int[] Month { get { return month; } set { month = value; } }            public int[] month = new int[12];        }        public List<UnusedRoles> unusedroles = new List<UnusedRoles>();        public List<UnusedRolesOccurrence> unusedrolesoccurrence = new List<UnusedRolesOccurrence>();        public Form1()        {            InitializeComponent();        }
查看完整描述

2 回答

?
弒天下

TA貢獻1818條經驗 獲得超8個贊

您的月份數組是靜態的,不要這樣做。

在您設置的并發操作期間,您的類的以下成員由該類UnusedRolesOccurrence所有實例共享。

private static int[] month = new int[12];

刪除 static 關鍵字,每個實例UnusedRolesOccurrence都會有自己的.month數組。

注意:您可能在此代碼中還有其他問題,但您的問題來自這里的這個問題。


查看完整回答
反對 回復 2022-01-09
?
牛魔王的故事

TA貢獻1830條經驗 獲得超3個贊

它正在以這種方式工作。


前:


        int[] arr_month = new int[12];


        var tmp = unusedroles.Select(x => new { x.Username, x.Role }).Distinct();


        foreach (var item in tmp)

        {

            unusedrolesoccurrence.Add(new UnusedRolesOccurrence

            {

                //Username = item.Username, Role = item.Role, Month = dictionary_month

                Username = item.Username, Role = item.Role, Month = arr_month

            });

        }

后:


    var tmp = unusedroles.Select(x => new { x.Username, x.Role }).Distinct();


    foreach (var item in tmp)

    {

        unusedrolesoccurrence.Add(new UnusedRolesOccurrence

        {

            //Username = item.Username, Role = item.Role, Month = dictionary_month

            Username = item.Username, Role = item.Role, Month = new int[12]

        });

    }


查看完整回答
反對 回復 2022-01-09
  • 2 回答
  • 0 關注
  • 194 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號