Skip to content

Instantly share code, notes, and snippets.

@MaciekSwiszczowski
Created August 11, 2019 17:36
Show Gist options
  • Save MaciekSwiszczowski/9ab3b1ffe173ca54cb0c9d73475f1be8 to your computer and use it in GitHub Desktop.
Save MaciekSwiszczowski/9ab3b1ffe173ca54cb0c9d73475f1be8 to your computer and use it in GitHub Desktop.
Creating new thread with WPF window
class Program
{
static void Main(string[] args)
{
var observable = Observable
.Interval(TimeSpan.FromMilliseconds(500))
.TakeWhile(counter => counter < 10);
var thread = new Thread(() =>
{
new TestWindow(observable);
Dispatcher.Run();
});
thread.SetApartmentState(ApartmentState.STA);
thread.IsBackground = true;
thread.Start();
Console.ReadKey();
}
public class TestWindow : Window
{
public TestWindow(IObservable<long> observable)
{
var isDispatcherInitialized = false;
Dispatcher.Invoke(() => isDispatcherInitialized = true, DispatcherPriority.ApplicationIdle);
if (!isDispatcherInitialized)
throw new ApplicationException("Dispatcher not initialized");
observable
.ObserveOnDispatcher()
.Window(TimeSpan.FromMilliseconds(600))
.Subscribe(_ => Console.WriteLine($"OnNext, observed on dispatcher with Window operator"));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment