0

I have one WPF Project. I want to display a TabControl, and its tabitem will be initialize dynamically, and tabitem's content is ChromiumWebBrowser. My main class is:

public partial class MainWindow : Window
    {
        public ChromiumWebBrowser browser;
        public MainWindow()
        {
            InitializeComponent();

            // init browser 
            browser = new ChromiumWebBrowser("https://test.vn");
            RequestContextSettings requestContextSettings = new RequestContextSettings();

            requestContextSettings.PersistSessionCookies = false;
            requestContextSettings.PersistUserPreferences = false;

            browser.Name = "B1";
            browser.MinHeight = 0;
            browser.RequestContext = new RequestContext(requestContextSettings);
            browser.LoadingStateChanged += Browser_LoadingStateChanged;
         }
          private void Browser_LoadingStateChanged(object sender, LoadingStateChangedEventArgs e)
        {

                ChromiumWebBrowser tmpB = (ChromiumWebBrowser)sender;
                Int16 tmpCount = Convert.ToInt16(tmpB.MinHeight);
        }
private void DynamicTab_Loaded(object sender, RoutedEventArgs e)
        {
            TabItem tmpTI = new TabItem();

            tmpTI.Content = this.browser;
            tmpTI.Header = "TEST1";
            this.dynamicTab.Items.Add(tmpTI);

        }

and WPF xml code:

<Window x:Class="WpfApp2.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:wpf="clr-namespace:CefSharp.Wpf;assembly=CefSharp.Wpf"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApp2"
        mc:Ignorable="d"
        Title="Tools" Height="1040.761" Width="1743.478" WindowState="Maximized">
    <Grid>
        <Border Grid.Row="1" BorderBrush="Gray" BorderThickness="0,1">            
            <TabControl Name="dynamicTab" Loaded="DynamicTab_Loaded" SelectionChanged="DynamicTab_SelectionChanged">

            </TabControl>
        </Border>
    </Grid>
</Window>

and the error was occurred at line Int16 tmpCount = Convert.ToInt16(tmpB.MinHeight);. The error message is: The calling thread cannot access this object because a different thread owns it.

Thank for your help!

Rong Nguyen
  • 4,143
  • 5
  • 27
  • 53
  • See http://cefsharp.github.io/api/79.1.x/html/E_CefSharp_Wpf_ChromiumWebBrowser_LoadingStateChanged.htm to access the UI you need to use the dispatcher. https://learn.microsoft.com/en-us/dotnet/api/system.windows.threading.dispatcher.begininvoke?view=netcore-3.1 – amaitland Jun 03 '20 at 20:11
  • Does this answer your question? [The calling thread cannot access this object because a different thread owns it](https://stackoverflow.com/questions/9732709/the-calling-thread-cannot-access-this-object-because-a-different-thread-owns-it) – Keith Stein Jun 03 '20 at 21:13
  • @amaitland can you suggest some examples ? – Rong Nguyen Jun 04 '20 at 02:10
  • https://stackoverflow.com/a/49545134/4583726 – amaitland Jun 04 '20 at 03:48

0 Answers0