public Worker_Chrome(string workerName, CancellationToken cancelToken) { this.WorkerName = workerName; chrome = new Chrome(workerName); t = new Thread(() => { chrome.WarmUp(); HlidacStatu.Util.InfinitiveTask.Run((tag) => { string swr = null; if (Workers.UrlToProcess.TryDequeue(out swr)) { Chrome.logger.Debug(workerName + ": dequeued " + swr); Workers.WorkRequest wr = Workers.WorkRequest.FromString(swr); if (wr != null) { //Workers.Results[swr] = Url2Png.Screenshot(chrome, wr.Url, wr.cropWidth, wr.cropHeight); Chrome.logger.Debug(workerName + ": getting screenshot " + swr); byte[] result = Url2PngSingle_Chrome.Screenshot(chrome, wr.Url, wr.cropWidth, wr.cropHeight); Workers.Results.AddOrUpdate(swr, result, (k, upd) => result); Chrome.logger.Debug(workerName + ": saved screenshot " + swr); } else { Chrome.logger.Error(workerName + ": Wrong decryption of " + swr); } } return(InfinitiveTask.TaskNextRunStatus.Continue); }, workerName, TimeSpan.FromMilliseconds(200), cancelToken); // fire every 200ms } ); t.Start(); }
public static async Task <byte[]> ScreenshotAsync(string url, TimeSpan timeout, int cropWidth = 1000, int cropHeight = 1000) { DateTime start = DateTime.Now; DateTime end = start.Add(timeout); byte[] result = null; Workers.WorkRequest wr = new Workers.WorkRequest() { Url = url, cropWidth = cropWidth, cropHeight = cropHeight }; string wrs = wr.ToString(); Workers.UrlToProcess.Enqueue(wrs); do { if (Workers.Results.ContainsKey(wrs)) { if (Workers.Results.TryRemove(wrs, out result)) { return(result); } else { System.Threading.Thread.Sleep(5); if (Workers.Results.TryRemove(wrs, out result)) { return(result); } else { Chrome.logger.Warning("Cannot get result"); } } //break; } await System.Threading.Tasks.Task.Delay(200); } while (end > DateTime.Now); return(result); }