Created
August 11, 2019 17:36
-
-
Save MaciekSwiszczowski/9ab3b1ffe173ca54cb0c9d73475f1be8 to your computer and use it in GitHub Desktop.
Creating new thread with WPF window
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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