'Can't show a wpf win called by static async function
I've an C#/Wpf application who needs to call a static class as soon as possible ( in Initialized event ).
The static class starts a process and during this process I need to have some WPF windows.
If all process in my static class is synchronous => no problem all works fine!
But I need to have the process asynchronous to not block the application.
As soon as I use a async function from library or use async/await, my windows are created but not show, I just see an icon in task bar and a black rectangle if I put the mouse over this icon.
If I do the same (asynchronous) from a button in my application all works fine !
I try many, many thinks with thread or dispatcher but nothing works.
Here is some code snippet:
Application:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void OnMainInitialized( object sender, EventArgs e ) // THIS DON'T WORKS!
{
Updater.Start( http://10.63.4.48:8080/UpdaterFiles/QM_Updater_Test/QM_Updater_Test.xml" );
}
private void Button_Click( object sender, RoutedEventArgs e ) // THIS WORKS!
{
Updater.Start( "http://10.63.4.48:8080/UpdaterFiles/QM_Updater_Test/QM_Updater_Test.xml" );
}
}
And static class :
public static class Updater
{
public static void Start( string Url )
//Ask_Win Dlg = new Ask_Win(); //IF UNCOMMENTED ALWAYS SHOW THE WINDOW (sync or async !!)
//Dlg.Show();
string LocalXml = System.IO.Path.GetDirectoryName( System.Reflection.Assembly.GetExecutingAssembly().Location ) + "\\Updates.xml";
if ( File.Exists( LocalXml ) ) ParseLocalXml( LocalXml );
if ( !CheckForInternetConnection() ) return;
m_Xml_Uri = new Uri( Url );
if ( sync ) { // SYNCHRONNOUS WAY => ALWAYS OK
if ( CheckUpdateSync() ) {
bool DoTheJob = true;
if ( !m_MandatoryUpdate ) DoTheJob = AskForUpdate();
if ( DoTheJob ) DoUpdate();
}
}
else
GetRemoteXmlAsync(); // SYNCHRONOUS WAY => ALWAYS BAD IF CALLED BY APPLICATION INITITIALIZED
}
private static void GetRemoteXmlAsync()
{
using ( WebClient Wc = GetWebClient( null ) ) {
Wc.DownloadStringCompleted += OnRemoteXmlCompleted;
Wc.DownloadStringAsync( m_Xml_Uri );
}
}
private static void OnRemoteXmlCompleted( object sender, DownloadStringCompletedEventArgs EventArgs )
{
try {
string Xml = EventArgs.Result;
if ( CheckRemoteXml( Xml ) ) {
bool DoTheJob = true;
if ( !m_MandatoryUpdate ) DoTheJob = AskForUpdate();
if ( DoTheJob ) DoUpdate();
}
}catch (Exception e ) {
ShowException( e );
}
}
//HERE IS THE FATAL FUNCTION !!!
private static bool AskForUpdate()
{
bool Ret = false;
using ( Ask_Win AskWin = new Ask_Win() ) {
if ( !string.IsNullOrEmpty( m_UpdateText ) ) AskWin.SetText( m_UpdateText );
if ( AskWin.ShowDialog() == true ) Ret = true;
}
return Ret;
}
}
To resume :
- If AskForUpdate is called without any async function => all ik OK
- If AskForUpdate is called after one async function => ShowDialog show nothing but the modal win is created and started ( => no action possible because it's a modal dialog)
- If AskForUpdate is called after one async function BUT from a button is main application => all is OK
I try these kind of thinks : Dispatcher.Invoke( () => ShowDialog() ); from static class, from wpf form, ...
I try to store the dispatcher in the beginning of the function , ....
No results until now !!!
Any ideas are welcome !
Thanks
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
