I had created a Panel that contains a Label and a CefSharp instance. The panel is then added to the WinForms. The purpose of the label is to show something before the browser finishes loading. Once the browser finished loading, I want the visible property of the label to be false.
But I get an error:
System.InvalidOperationException: 'Cross-thread operation not valid: Control 'Loading' accessed from a thread other than the thread it was created on.'
How can I solve it?
This is the method that creates all the controls.
chromeBrowser = new CustomWebBrowser("https://www.google.com/");
chromeBrowser.FrameLoadEnd += OnBrowserFrameLoadEnd;
Panel panel = new Panel()
{
Name = "Container",
Dock = DockStyle.Fill,
BackColor = Color.Aquamarine,
Padding = new Padding(5),
Margin = new Padding(2),
};
Label label = new Label
{
Name = "Loading",
Dock = DockStyle.Fill,
BackColor = Color.Black,
ForeColor = Color.White,
Padding = new Padding(5),
Margin = new Padding(2),
};
// Add reference
chromeBrowser.Label = label;
panel.Controls.Add(chromeBrowser);
panel.Controls.Add(chromeBrowser.Label);
panel.Controls.SetChildIndex(chromeBrowser.Label, 0);
//Add the browser to the Form
this.Controls.Add(panel);
The OnBrowserFrameLoadEnd event handler
private void OnBrowserFrameLoadEnd(object sender, FrameLoadEndEventArgs args)
{
CustomWebBrowser wb = (CustomWebBrowser)sender;
if(wb.Label != null)
{
wb.Label.Visible = false;
MessageBox.Show("Done");
}
}