如果我有這個private static List<Action> actions = new List<Action>();private static void Main(){ for (var i = 0; i < 10; i += 1) { Action action = async () => { // want to remove this specific lambda item from the "actions" variable. // is there something like this: /* Action this_action = this; actions.Remove(this_action); */ }; actions.Add(action); } Parallel.Invoke(actions.ToArray()); Console.Write("\nPress any key to exit..."); Console.ReadKey();}如何從列表中正確刪除它。我需要某種自我參考?
1 回答

皈依舞
TA貢獻1851條經驗 獲得超3個贊
您可以僅使用 to 的引用action來刪除操作的實例。關閉將確保引用指向正確的對象。
for (var i = 0; i < 10; i += 1)
{
Action action = null;
action = () => {
actions.Remove(action);
};
actions.Add(action);
}
Parallel.Invoke(actions.ToArray());
有關工作示例,請參閱Fiddle。
- 1 回答
- 0 關注
- 500 瀏覽
添加回答
舉報
0/150
提交
取消