Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Window.Activate() not working properly

Tags:

c#

.net

wpf

Sometime, Window.Activate() not working in other system.

var window = Application.Current.Windows.OfType<Window>().FirstOrDefault(y => y.IsActive) ?? Application.Current.Windows.OfType<Window>().FirstOrDefault();
if (window != null) 
    window.Activate();

I want a solution which should not fail in any system and also want to know the main cause of this.

like image 968
Gorakh Nath Avatar asked Jul 02 '26 23:07

Gorakh Nath


1 Answers

It can fail if you call it from non-UI thread. In that case you should to wrap call into dispatcher.

Application.Current.Dispatcher.Invoke(() =>
{       
    ///.... UI stuff
});
like image 138
tym32167 Avatar answered Jul 05 '26 12:07

tym32167