public Form1()
{
InitializeComponent();
InitializeChromium();
browser.FrameLoadEnd += WebBrowserFrameLoadEnded;
}
private void WebBrowserFrameLoadEnded(object sender, FrameLoadEndEventArgs e)
{
Task.Run(async () => html = await browser.GetSourceAsync());
MessageBox.Show(html); // html = null
}
а ты про область видимости в книге проходил главу? Я не особо вижу нужды использовать task run тут
а ты про область видимости в книге проходил главу? Я не особо вижу нужды использовать task run тут
Task.Run(async () => html = await browser.GetSourceAsync());
MessageBox.Show(html); // html = null
html = await browser.GetSourceAsync()
MessageBox.Show(html);
using System;
using CefSharp;
using HtmlAgilityPack;
using CefSharp.OffScreen;
using System.Windows.Forms;
using System.Threading;
using System.Net;
using System.Threading.Tasks;
public partial class Form1 : Form
{
private ChromiumWebBrowser browser;
private string html;
HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
HtmlNodeCollection node;
public Form1()
{
InitializeComponent();
InitializeChromium();
browser.FrameLoadEnd += WebBrowserFrameLoadEnded;
}
private void button1_Click(object sender, EventArgs e)
{
browser.Load($"https://kad.arbitr.ru/Card?number={textBox1.Text}");
Number.Text = "Номер: " + textBox1.Text;
}
private async void WebBrowserFrameLoadEnded(object sender, FrameLoadEndEventArgs e)
{
html = await browser.GetSourceAsync().ConfigureAwait(false);
MessageBox.Show(html);
if (html != null)
{
doc.LoadHtml(html);
node = doc.DocumentNode.SelectNodes("//*[@id='b-case-header']/ul[2]/li[4]/a");
SetTextToControl(Plaintiffs, "Истцы:\n" + node[0].InnerText.Trim());
}
private void InitializeChromium()
{
CefSettings settings = new CefSettings();
Cef.Initialize(settings);
browser = new ChromiumWebBrowser("https://kad.arbitr.ru");
//Controls.Add(browser);
//browser.Dock = DockStyle.Fill;
}
private void SetTextToControl(Control control, string text)
{
Invoke(new Action(() => control.Text = text));
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
Cef.Shutdown();
}
у вас browser.Load асинхронный метод, сделать обработчик кнопки async и await browser.Load и убрать ConfigureAwait(false)
using System;
using CefSharp;
using HtmlAgilityPack;
using CefSharp.WinForms;
using System.Windows.Forms;
using System.Threading;
using System.Net;
using System.Threading.Tasks;
namespace WinFormsApp1
{
public partial class Form1 : Form
{
private ChromiumWebBrowser browser;
// HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
// HtmlNodeCollection node;
public Form1()
{
InitializeComponent();
InitializeChromium();
browser.FrameLoadEnd += Browser_FrameLoadEnd;
}
private async void Browser_FrameLoadEnd(object sender, FrameLoadEndEventArgs e)
{
string html = await browser.GetSourceAsync(); // тут все умирает...
MessageBox.Show(html);
}
private void button1_Click(object sender, EventArgs e)
{
browser.Load($"https://kad.arbitr.ru/Card?number={textBox1.Text}");
}
private void InitializeChromium()
{
CefSettings settings = new CefSettings();
Cef.Initialize(settings);
browser = new ChromiumWebBrowser("https://kad.arbitr.ru");
Controls.Add(browser);
browser.Dock = DockStyle.Fill;
}
private void Form1_FormClosed(object sender, FormClosedEventArgs e)
{
Cef.Shutdown();
}
}
}
я открыл класс ChromiumWebBrowser
А типо... А как? Причем тут StringVisitor?
public interface IStringVisitor : IDisposable
{
/// <summary>
/// Method that will be executed.
/// </summary>
/// <param name="str">string (result of async execution)</param>
void Visit(string str);
}
public class TaskStringVisitor : IStringVisitor
{
private string CompletionSource;
/// <summary>
/// Method that will be executed.
/// </summary>
/// <param name="str">string</param>
void IStringVisitor.Visit(string str)
{
CompletionSource = str;
}
/// <summary>
/// Task that can be awaited for the result to be retrieved async
/// </summary>
public string resultic
{
get { return CompletionSource; }
}
void IDisposable.Dispose()
{
}
}
private void Browser_FrameLoadEnd(object sender, FrameLoadEndEventArgs e)
{
browser.GetSourceAsync().ContinueWith(taskHtml =>
{
var html = taskHtml.Result;
MessageBox.Show(html);
});
}
var html = browser.ExecuteScriptAsync("document.documentElement.outerHTML");
это слишком сложно для меня