我正在为游戏制作服务器,我需要保存每个人的第二个,所以我制作了:-
private static void SubmitChanges() {
while(true) {
try {
using(GameDBDataContext db = new GameDBDataContext()){
foreach(IHero hero in world.Entities.BattleEntities.OnlineHeros.Values) {
hero.Update(db);
}
}
} catch(Exception ex) {
Console.WriteLine(ex.ToString());
}
Thread.Sleep(1000);
}
}
可以吗?如果我有500个在线英雄会很好吗?
编辑:检查需要多长时间:-
private void UpdateDatabase() {
try {
using(GameDBDataContext db = new GameDBDataContext()) {
foreach(IHero hero in world.Entities.BattleEntities.OnlineHeros.Values) {
DateTime now = DateTime.Now;
hero.Update(db);
DateTime after = DateTime.Now;
Console.WriteLine((now - after).Milliseconds);
}
}
} catch(Exception ex) {
Console.WriteLine(ex.ToString());
}
}
结果 :-
-13
-10
-13
-26
-19
-24
-25
-19
-27
-22
-19
-26
-25
-21
-24
-22