private static void TranslateResourceData() { using (var service = new ResourceService3()) { var dataList = service.GetResourceNeedTranslate(); if (dataList != null) { var currenTask = new List <Task>(); var threadCount = TranslatorService3.ServerList.Count; var count = dataList.Count / threadCount; for (var serverID = 0; serverID < threadCount; serverID++) { var id = serverID; currenTask.Add(Task.Factory.StartNew(() => { if (id < threadCount) { var forLast = dataList.Count - (id * count); var range = dataList.GetRange(count * id, id + 1 == threadCount ? forLast : count); Console.WriteLine("Start Thread " + id + $" List: {range.Count}"); TrasnaltePerServer(range, id); } })); } Task.WaitAll(currenTask.ToArray()); } } }
private static void TrasnaltePerServer(List <GetWordForTranslate_Result> dataList, int serverId) { foreach (var data in dataList) { var dateTime = DateTime.Now; using (var service2 = new ResourceService3()) { if (!string.IsNullOrEmpty(data.Word) && "en" != data.LanguageCode.ToLower()) { try { var resultT = TranslateGetAll( "en", data.LanguageCode, data.Word, serverId); resultT.Wait(); data.Translated = resultT.Result; var objectT = JsonConvert.DeserializeObject <CallBankService>(data.Translated); if (!string.IsNullOrEmpty(data.Translated) && !string.IsNullOrEmpty(objectT.Text)) { // objectT. objectT.All = ProcessTranslateFiles2(new Result1() { WordId = data.WordID, LanId = data.LangId, Word = data.Word, Raw = objectT.Raw, First = "", }); service2.SaveResourceTranslated(data, objectT); Console.ForegroundColor = ConsoleColor.White; Console.WriteLine( $"{serverId}\t{TranslatorService3.ServerList[serverId]}\t{data.Word}\t{data.LanguageCode}\t{objectT.Text}\t{DateTime.Now:HH:mm:ss}"); } } catch (Exception e) { Console.WriteLine(e.Message); } } } var waitMilisecond = 1400; var sleep = (DateTime.Now - dateTime).TotalMilliseconds; //Console.WriteLine($"Time To call all {sleep}"); if (sleep < waitMilisecond) { var wait = waitMilisecond - (int)sleep; Task.Delay(wait).Wait(); //Console.WriteLine($"Time need wait {wait}"); var rondomNumber = new Random().Next(1, 100); Task.Delay(rondomNumber).Wait(); } } if (serverId == 0) { System.Diagnostics.Process.Start(Assembly.GetExecutingAssembly().Location); Environment.Exit(-1); } }