私は C# と Threading の初心者で、これは非常に単純な質問ですが、本当に行き詰まっています。このサイトで検索しましたが、私のシナリオに似た答えが見つかりませんでした:
Parent() というメソッドがあり、その中で型付きリストを作成し、n 回ごとにそれを Task に渡します。リストが増え続けるため、いつリストをクリアしてメモリを解放するかという問題があります。タスクの最後にリストをクリアしようとしましたが、Parent メソッドでリストをクリアすると、スレッドでリストが空になります。
誰かが私を助けることができますか?これは非常に単純な問題であることは承知していますが、助けていただければ幸いです。
public void Parent()
{
List<MyType> list = new List<MyType>();
for (int i = 0; i< N; i++)
{
list.Add(new MyType {Var = "blah"});
if ( i% 10 == 0) //every tentth time we send a task out tou a thread
{
Task.Factory.StartNew(() => WriteToDB(new List<MyType>(list)));
//here I am sending a new instance of the list
//Task.Factory.StartNew(() => WriteToDB((list)));
//here I am sending same instance
list.Clear();
//if I clear here the list sent to the WriteToDB is empty
//if I do not, the memory keeps growing up and crashes the app
}
private void WriteToDB(List<MyType> list)
{
//do some calculations with the list
//insert into db
list.Clear();
}
}
}