Reputation: 3716
I know this was answered but i can't get it to work. can someone be kind enough to help me figure this out
Here's my xaml class:
namespace Windamow
{
/// <summary>
/// Interaction logic for DynamoWindow.xaml
/// </summary>
public partial class DynamoWindow : Window
{
public DynamoWindow()
{
InitializeComponent();
}
public void setBrowserURL(string URL)
{
browser.Source = new Uri(URL);
}
public void setBrowserFromString(string HTMLString)
{
browser.NavigateToString(HTMLString);
}
}
}
I then try to update the html string that gets displayed like this:
namespace Windamow
{
public class Windamow
{
private DynamoWindow window;
internal void ThreadProc()
{
window = new DynamoWindow();
window.ShowDialog();
}
internal Windamow()
{
Thread t = new Thread(ThreadProc);
t.SetApartmentState(ApartmentState.STA);
t.Start();
}
public static DynamoWindow MakeWindow(bool launch, string html)
{
if (launch)
{
Windamow mow = new Windamow();
var action = new Action(() => mow.window.setBrowserFromString(html));
Application.Current.Dispatcher.BeginInvoke(
DispatcherPriority.Input,
action);
return mow.window;
}
else
{
return null;
}
}
}
}
xaml:
<Window x:Class="Windamow.DynamoWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<Grid>
<WebBrowser x:Name="browser" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"/>
</Grid>
Error message:
The calling thread cannot access this object because a different thread owns it.
Upvotes: 1
Views: 117
Reputation: 13408
The issue is here:
var action = new Action(() => mow.window.setBrowserFromString(html));
This code will try to access the window
object from the main UI thread whereas it has been created on and associated to another thread, the one you create yourself t
.
Depending on your use case you may simply try something like:
public DynamoWindow(string html)
{
InitializeComponent();
setBrowserFromString(html);
}
...
if (launch)
{
Windamow mow = new Windamow(html);
return mow.window;
}
(not tested but you get the idea)
Upvotes: 1