private int Send(List <VKApiRequestModel> Requests, SenderContext context) { var res = AsyncHelper.RunSync(() => vk.SendAllRequests(Requests)); if (res == 0) { context.SaveChanges(); } else { context.UndoChanges(); } Requests.Clear(); return(res); }
public void DoWork() { var context = new SenderContext("database"); var Requests = new List <VKApiRequestModel>(); while (true) { try { var list = context.Message.Where(x => !x.IsSended && x.BotUserVKID != null).ToArray(); int count = 0; //неотправленные запросы пользователям Вконтакте foreach (var item in list) { count++; if (item.Photo != null) { Requests.Add(new VKApiRequestModel(ulong.Parse(item.BotUserVKID), item.Text, RequestType.SendMessageWithPhoto, item.Photo)); } else { Requests.Add(new VKApiRequestModel(ulong.Parse(item.BotUserVKID), item.Text)); } item.IsSended = true; if (count >= 20 && count % 20 == 0) { var res = AsyncHelper.RunSync(() => vk.SendAllRequests(Requests)); if (res == 0) { context.SaveChanges(); } else { context.UndoChanges(); } Requests.Clear(); } } if (Requests.Count > 0) { var res = AsyncHelper.RunSync(() => vk.SendAllRequests(Requests)); if (res == 0) { context.SaveChanges(); } else { context.UndoChanges(); } Requests.Clear(); Thread.Sleep(500); } else { Thread.Sleep(500); } } catch (Exception esc) { Logging.Logger.Error(esc); } } }