public void Return(IVirtualBrowser browser) { if (IsPoolEnabled) { browser.Size = new Size(1920, 1080); _freeBrowsers.Add(browser); } else { browser.Dispose(); } _browserPool.Release(1); }
public virtual async Task <bool> LoadPageAsync(IVirtualBrowser browser, string processJsonModel, int maxImageWidth, int maxImageHeight) { try { var task = new TaskCompletionSource <bool>(); if (!_tcs.TryAdd(browser, task)) { Debug.Assert(false, "Unexpected Error: The dictionary does not contain a key (browser) added previously."); } var htmlPath = Path.GetFullPath("ProcessHtml/index.html"); browser.AsyncBoundObject.Reset(RenderWaitSeconds * 1000); browser.LoadingStateChanged += BrowserLoadingStateChanged; browser.Load(htmlPath); await task.Task; } finally { TaskCompletionSource <bool> removedTask; if (!_tcs.TryRemove(browser, out removedTask)) { Debug.Assert(false, "Unexpected Error: The dictionary already contains a key (browser)."); } } //------------------------------------------- // Get Process model browser.ExecuteScriptAsync($"window.renderGraph('{HttpUtility.JavaScriptStringEncode(processJsonModel)}', {maxImageWidth}, {maxImageHeight});"); bool renderResult; try { renderResult = await browser.AsyncBoundObject.RenderCompletionSource.Task; } catch (TaskCanceledException) { throw new ApplicationException(ServiceHelper.RenderTimeoutErrorMessage); } if (!renderResult) { throw new ApplicationException(browser.AsyncBoundObject.ErrorMessage); } Log.Info($"Cef Image: width = {browser.AsyncBoundObject.Width}, height = {browser.AsyncBoundObject.Height}, scale = {browser.AsyncBoundObject.Scale}"); var w = browser.AsyncBoundObject.Width + 20; var h = browser.AsyncBoundObject.Height + 20; browser.Size = new Size(w, h); //----------------------------------------------------------------------------------------- // The way how to detect when the browser resizing is completed, with the timeout. for (var i = 0; i < MaxWaitTimeSeconds * 1000 / DelayIntervalMilliseconds; i++) { if (browser.Bitmap != null && browser.Bitmap.Width == w && browser.Bitmap.Height == h) { break; } await Task.Delay(DelayIntervalMilliseconds); } //----------------------------------------------------------------------------------------- return(true); }