use the following code in the current thread to call wpf window in another thread.
public void Calling method() // this method is in current thread
{
CommonData.StaticObjects.IntPublisher = new Publisher<string>(); // initialize publisher
Thread thread = new Thread(() =>
{
Wpf_Ui.MainWindow objwindowa = new Wpf_Ui.MainWindow();
objwindowa.Show();
objwindowa.Closed += (sender2, e2) =>
objwindowa.Dispatcher.InvokeShutdown();
System.Windows.Threading.Dispatcher.Run();
});
thread.SetApartmentState(ApartmentState.STA);
thread.Start();
CommonData.StaticObjects.IntPublisher.PublishData("outside while loop1");
Thread.Sleep(500);
CommonData.StaticObjects.IntPublisher.PublishData("outside while loop1");
Thread.Sleep(500);
CommonData.StaticObjects.IntPublisher.PublishData("outside while loop1");
CommonData.StaticObjects.IntPublisher.PublishData("Func executed");
Thread.Sleep(500);
CommonData.StaticObjects.IntPublisher.PublishData("Going to End");
Thread.Sleep(500);
}
// the below class will be the mainwindow.cs of mainwindow.xaml
public partial class MainWindow : Window
{
public delegate void UpdateTextCallback(string valuesend);
string value;
private readonly Subscriber<string> IntSublisher1;
public MainWindow()
{
InitializeComponent();
IntSublisher1 = new Subscriber<string>(StaticObjects.IntPublisher);
IntSublisher1.Publisher.DataPublisher += publisher_DataPublisher1;
}
private void publisher_DataPublisher1(object sender, MessageArgument<string> e)
{
value = e.Message;
lblMessage.Dispatcher.Invoke(
new UpdateTextCallback(this.UpdateText),
new object[] { value.ToString() }
);
}
public void UpdateText(string valuesend)
{
lblMessage.Content = valuesend;
}
}
so wpf window can access the published datas by subscribing events inside the class needed, and thus invoking the specific wpf ui component will update the message in the screen in real time.
thus messages were shared between two threads.
to know how publish and subscribe pattern works click here
public void Calling method() // this method is in current thread
{
CommonData.StaticObjects.IntPublisher = new Publisher<string>(); // initialize publisher
Thread thread = new Thread(() =>
{
Wpf_Ui.MainWindow objwindowa = new Wpf_Ui.MainWindow();
objwindowa.Show();
objwindowa.Closed += (sender2, e2) =>
objwindowa.Dispatcher.InvokeShutdown();
System.Windows.Threading.Dispatcher.Run();
});
thread.SetApartmentState(ApartmentState.STA);
thread.Start();
CommonData.StaticObjects.IntPublisher.PublishData("outside while loop1");
Thread.Sleep(500);
CommonData.StaticObjects.IntPublisher.PublishData("outside while loop1");
Thread.Sleep(500);
CommonData.StaticObjects.IntPublisher.PublishData("outside while loop1");
CommonData.StaticObjects.IntPublisher.PublishData("Func executed");
Thread.Sleep(500);
CommonData.StaticObjects.IntPublisher.PublishData("Going to End");
Thread.Sleep(500);
}
// the below class will be the mainwindow.cs of mainwindow.xaml
public partial class MainWindow : Window
{
public delegate void UpdateTextCallback(string valuesend);
string value;
private readonly Subscriber<string> IntSublisher1;
public MainWindow()
{
InitializeComponent();
IntSublisher1 = new Subscriber<string>(StaticObjects.IntPublisher);
IntSublisher1.Publisher.DataPublisher += publisher_DataPublisher1;
}
private void publisher_DataPublisher1(object sender, MessageArgument<string> e)
{
value = e.Message;
lblMessage.Dispatcher.Invoke(
new UpdateTextCallback(this.UpdateText),
new object[] { value.ToString() }
);
}
public void UpdateText(string valuesend)
{
lblMessage.Content = valuesend;
}
}
so wpf window can access the published datas by subscribing events inside the class needed, and thus invoking the specific wpf ui component will update the message in the screen in real time.
thus messages were shared between two threads.
to know how publish and subscribe pattern works click here
No comments:
Post a Comment
frndz giv ur commentz..