これをできるだけシンプルに保つようにします。これが私の方法です。開始するだけです-以下のコードが間違っていることは理解しています-これは私が現時点で持っているものです:
public static void GetActorsFromCastList(TmdbMovieCast cast)
{
// use timers here
List<Cast> liCast = cast.cast;
Timer actorTimer = new Timer(1000);
// put the below into a foreach loop to get a new personId each time???
foreach (var i in liCast)
{
actorTimer.Elapsed += new ElapsedEventHandler((sender, e) => RunActorEvent(sender, e, i.id));
actorTimer.Start();
}
}
public static void RunActorEvent(object sender, ElapsedEventArgs e, int personId)
{
// run a single API call here to get a Person (actor)
_actors.Add(_api.GetPersonInfo(personId));
}
ご覧のSystem.Timer
とおり、上記のように を作成しました。アイデアは、毎秒を呼び出して、RunActorEvent
毎回異なるものを渡すPersonId
ことです。最終的な目標は、毎秒 1 回呼び出すことですがRunActorEvent
、そのたびに新しいPersonId
. ElapsedEventHandler
3 番目のパラメーターを追加したものを既に作成しましたPersonId
。
それが私がいるところです。私が抱えているジレンマは、これが正しく見えないということです。つまり、反復ごとに基本的に新しい ElapsedEventHander を作成するforeach
ループがあり、これは設計すべきではないと思います。
質問System.Timer
: と を作成し、が呼び出されるたびに新しい変数 ( ) を(イベント ハンドラー) にElapsedEventHandler
渡すにはどうすればよいですか?PersonId
RunActorEvent
ElapsedEventHander