Hello there,
Again, need your hands.
I have one WPF application that will invoke some methods hosted in second AppDomain, now, I want to access one WPF control (ProgressBar) from one separate class file(not code-behind), which extends from MarshalByRefObject, I need to do this like below:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace WpfListenerCrossDomain
{
public class EventSink : MarshalByRefObject
{
public void WatchProgress(object sender, EventArgs e)
{
//Console.WriteLine("There is a Progress!!");
// Here, I need to update wpfProgressBar, how to do?
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using WpfRemoteServer;
using System.Reflection;
namespace WpfListenerCrossDomain
{
/// <summary>
/// Interaction logic for Window1.xaml
/// </summary>
public partial class Window1 : Window
{
EventSink m_sink;
public Window1()
{
InitializeComponent();
m_sink = new EventSink();
}
private void btnCreate2domain_Click(object sender, RoutedEventArgs e)
{
AppDomain ad2 = AppDomain.CreateDomain("domain2", null, null);
SampleRemote romoteobj = (SampleRemote)ad2.CreateInstanceAndUnwrap("WpfRemoteServer", "WpfRemoteServer.SampleRemote");
romoteobj.UpdateProgressEvent += new WpfRemoteServer.SampleRemote.OnUpdatePregress(m_sink.WatchProgress);
romoteobj.MethodA();
}
}
}
<Window x:Class="WpfListenerCrossDomain.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300">
<Grid>
<Button Name="btnCreate2domain" Click="btnCreate2domain_Click">
</Button>
<ProgressBar Name="wpfProgressBar" Minimum="0" Maximum="100" Width="250" Height="25" Margin="20,0,0,0" />
</Grid>
</Window>
Another one (B):
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
namespace WpfRemoteServer
{
public class SampleRemote : MarshalByRefObject
{
public delegate void OnUpdatePregress(object sender, EventArgs e);
public event OnUpdatePregress UpdateProgressEvent;
public void MethodA()
{
Console.WriteLine("Running in domain:" + Thread.GetDomain().FriendlyName);
if (UpdateProgressEvent != null)
{
UpdateProgressEvent(this, new EventArgs());
}
}
}
}
Thanks, Kent