Click here to Skip to main content
15,891,136 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
We are trying to integrate existing WPF window on to Win32 container application. The win32 application can integrate and show only Winforms Usercontrols. Since we already have a running WPF window code, we used the element host and we used the content presenter of the window inside the element host to show as a winforms usercontrol.
The WPF window code overrides the function protected virtual void OnInitialized(EventArgs e) and calls first base.OnInitialized and then
the custom code.

The issue is here is that you can run the below code for the first instance from win32 container , but second instance when we execute same
code , it throws the error "The calling thread cannot access this object because a different thread owns it." from the line base.OnInitialized when initialization
AutoUpdate window = new Autoupdate(); 

Even if we use the below code under dispatcher it throws the same error after the firstime.
ElementHost elementHost = new ElementHost();
                      elementHost.Dock = DockStyle.None;
                      elementHost.Width = 150;
                      elementHost.Height = 50;
                      AutoUpdate window = new Autoupdate();
                      ContentPresenter pres = new ContentPresenter();
                      pres.Content = window.Content;
                      elementHost.Child = pres;
                      this.Controls.Add(elementHost);
Posted
Updated 12-Sep-10 5:39am
v2
Comments
Abhishek Sur 12-Sep-10 11:39am    
Always use pre tag to format the question properly.

1 solution

Well, actually ElementHost displays the whole WPF app within it.

I think, it does not allow you to update controls like ContentPresenter from outside(winforms appliction).

I think it would be a good practice to expose one method which will create this ContentPresenter for you.

Sacha Barbar proposed a good extension to work with Cross thread issues. You might take a look into it.
Check this :
C#
public static void InvokeIfRequired(this DispatcherControl control, Action operation)
{
  if (control.Dispatcher.CheckAccess())
  {
    operation();
  }
  else
  {
    control.Dispatcher.BeginInvoke(DispatcherPriority.Normal, operation);
  }
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900