I have requirement in which i need to show smooth scrolling text with some GIF Images and jpeg or mediaelement on a ticker. However, since this involves lot of CPU cycles for the main UI thread, i planned to create the ticker control on another thread with a dispatcher and then host this ticker on the form. However, i am getting a cross-thread exception that thread cannot access the control as another thread owns it.
I have done similar thing in Delphi, wherein i have set the ticker parent with SetWindowParent();
my code is as below
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
TickerControlContainer loclContainer = new TickerControlContainer(this);
}
}
public class TickerControlContainer
{
private MainWindow f_Window;
private void CreateControl()
{
TickerControl loclControl = new TickerControl();
loclControl.InitializeComponent();
f_Window.Dispatcher.Invoke((MethodInvoker)delegate { AddControl(loclControl); });
}
private void AddControl(TickerControl piclControl)
{
f_Window.Content = piclControl;
**// exception occurs**
}
public TickerControlContainer(MainWindow piclWindow)
{
f_Window = piclWindow;
ManualResetEvent loclResetEvent = new ManualResetEvent(false);
Dispatcher loclDispatcher = null;
Thread th1 = new Thread(new ThreadStart(() =>
{
loclDispatcher = Dispatcher.CurrentDispatcher;
loclResetEvent.Set();
Dispatcher.Run();
}));
th1.SetApartmentState(ApartmentState.STA);
th1.Start();
loclResetEvent.WaitOne();
loclDispatcher.BeginInvoke((MethodInvoker)delegate { CreateControl(); });
}
}
Do i need to put a contentcontrol or something on my form, instead of setting as the content of the form.
This is just a sample that i am trying to do. Please help.