有一个案例,应用系统需要每1分钟向在线用户发布消息,代码是使用multithead任务读取消息列表。但是程序运行不正确,会抛出一个 index out of range 异常。请有人可以提出任何建议,谢谢。
private Timer taskTimer;
private static readonly object _locker = new object();
private static IList<Message> _messages = null;
private void OnTimerElapsed(object sender)
{
var msgModel = new MessageModel();
_messages = msgModel.GetMessageList();
var msgCount = _messages.Count();
Task[] _tasks = new Task[msgCount];
for (int i = 0; i < msgCount; i++)
{
if (i < msgCount)
{
_tasks[i] = Task.Factory.StartNew(() =>
{
lock (_locker)
{
PushMessage(i);
}
});
}
}
//waiting all task finished
while (_tasks.Any(t => !t.IsCompleted)) { }
}
private void PushMessage(int i)
{
var msg = _messages[i]; //it will throw an exception here...
//send message to on line users.
SendToOnlineUsers(msg);
}
Error:
Index was out of range. Must be non-negative and less than the size of the collection.
StackTrace Details:
at System.ThrowHelper.ThrowArgumentOutOfRangeException()
at System.Collections.Generic.List`1.get_Item(Int32 index)
at WebIM.Hubs.BackgroudPushServiceTimer.PushMessage(Int32 i) in ...
at WebIM.Hubs.BackgroudPushServiceTimer.<>c__DisplayClass6.<OnTimerElapsed>b__2() in ...
at System.Threading.Tasks.Task.InnerInvoke()
at System.Threading.Tasks.Task.Execute()
如果消息数为 4,并且 PushMessage 函数中的索引也是 4,则超出范围。