Last active
September 4, 2015 03:04
-
-
Save devlights/70a1a5efc7d191dbec1e to your computer and use it in GitHub Desktop.
[WPF] 別スレッドでUIスレッドを立ててそこからWindowを表示
This file contains hidden or 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
| using System.Collections.Generic; | |
| using System.Linq; | |
| using System.Text; | |
| using System.Threading; | |
| using System.Threading.Tasks; | |
| 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 System.Windows.Threading; | |
| namespace 別スレッドでポップアップ表示 | |
| { | |
| /// <summary> | |
| /// 参照リソース | |
| /// http://reedcopsey.com/2011/11/28/launching-a-wpf-window-in-a-separate-thread-part-1/ | |
| /// https://eprystupa.wordpress.com/2008/07/28/running-wpf-application-with-multiple-ui-threads/ | |
| /// http://stackoverflow.com/questions/1111369/how-do-i-create-and-show-wpf-windows-on-separate-threads | |
| /// </summary> | |
| public partial class MainWindow : Window | |
| { | |
| public MainWindow() | |
| { | |
| InitializeComponent(); | |
| } | |
| private void Btn1_Click(object sender, RoutedEventArgs e) | |
| { | |
| var t = new Thread(() => | |
| { | |
| SynchronizationContext.SetSynchronizationContext( | |
| new DispatcherSynchronizationContext(Dispatcher.CurrentDispatcher) | |
| ); | |
| var newWindow = new Popup1(); | |
| newWindow.Closed += (o, args) => | |
| { | |
| Dispatcher.CurrentDispatcher.BeginInvokeShutdown(DispatcherPriority.Background); | |
| }; | |
| newWindow.Topmost = true; | |
| newWindow.Show(); | |
| Dispatcher.Run(); | |
| }); | |
| t.SetApartmentState(ApartmentState.STA); | |
| t.IsBackground = true; | |
| t.Start(); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment