locked
Async operation needs to be ready before move on

    Question

  • Hi everyone,

    i am going through a series of monogame tutorials and im facing some issues since the tutorial uses a desktop template and im running the Windows store version,  in order to get tasks done , i need to have asynchronous operation since Winrt is all about async, however im facing some dificulties at the moment, so i have a singleton class and the new instance of the singleton is implemented a little different from the usual way, it actually completes loading the new instance of the class through a xml file , so here's the deal:

    Singleton class sample :

    public class ScreenManager
        {
           
    
            private static ScreenManager _instance;
            
            public static ScreenManager Instance
            {
                get
                {
                    if (_instance == null)
                    {
                       SetScreenManager().GetAwaiter().GetResult();
                       
                    }
    
                    return _instance;
                }
                
            }
    
     public async static Task<ScreenManager> SetScreenManager()
            {
                XmlManager<ScreenManager> xml = new XmlManager<ScreenManager>();            
               var file = await StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appx:///Content/Load/ScreenManager.xml"));
                var str = await FileIO.ReadTextAsync(file);            _instance =  xml.Load(str);
                return _instance;
            }
        }

    and this is the XmlManager class used for serialization :

     public class XmlManager<T>
        {
    
            public Type Type { get; set; }
    
            public XmlManager()
            {
                Type = typeof(T);
            }
    
           
            public T Load(string path)
            {
                
                T instance;
           
                using (StringReader reader = new StringReader(path))
                {
                   
                    XmlSerializer xml = new XmlSerializer(Type);
                    instance = (T)xml.Deserialize(reader);
                }
                return instance;
            }
        }

    the problem is, when i call the SetScreenManager().GetAwaiter().GetResult(); from the getter at the top of the class, it doest finish with the method code then it returns to the _instance which is null at the time cause that method hasnt finished the serialization since it contains 2 awaits and its an asynchronous method, how should i proceed in order to have the SetScreenManager().GetAwaiter().GetResult(); finished before moving on to the return _instance of the getter?
    any help is greatly appreciated!!

    Kind regards,

    Romulo Romero



    PS: i tried this but seems obvious why it didnt work either :

     public static ScreenManager Instance
            {
                get
                {
                    if (_instance == null)
                    {
                         
               
                      Task.Run(async () => 
                          {
                              await SetScreenManager();
                          });
                        //_instance = new ScreenManager();
                    }
                    return _instance;
                }
                
            }

    Sunday, March 2, 2014 4:17 PM

Answers

All replies

  • i have tried this :

    public static ScreenManager Instance
            {
                get
                {
                    if (_instance == null)
                    {
                        var task =  SetScreenManager();
                        task.Wait();
                        var result = task.Result;
                        _instance = result;
    
                     }
                  
                        return _instance;
                    
             }

    But still no coins, it simply stucks there in the splash screen and never executes the app completely...even trying to debug it, when it goes to the task.wait() it gets out of the debugger..

    im still asynchronously await for a response to this thread lol

    thanks in advance for any help!

    Romulo Romero

    Sunday, March 2, 2014 10:11 PM
  • i tried this :

    private static ScreenManager _instance;
            
            public static ScreenManager Instance
            {
                get
                {
                    if (_instance == null)
                    {
                       
                        XmlManager<ScreenManager> xml = new XmlManager<ScreenManager>();
                        _instance = xml.Load(HandleFileAsync());
                   
                    }
                  
                        return _instance;
                    
                  
                }
                
            }
    
    
     public static async Task<string> ProcessDataAsync()
            {
                var file = await StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appx:///Content/Load/ScreenManager.xml"));
                str = await FileIO.ReadTextAsync(file);
                return str;
               
            }
            public static string HandleFileAsync()
            {
              
                var task = ProcessDataAsync();
                task.Wait();
                var result = task.Result;
                str = result;
               
               return str;
            }

    even this :

    private static ScreenManager _instance;
            
            public static ScreenManager Instance
            {
                get
                {
                    if (_instance == null)
                    {
                       
                       var task = new Task( async () => await ProcessDataAsync());
    		   task.Wait();
    		   var result = task.Result;
    		   _instance = result;
                   
                    }
                  
                        return _instance;
                    
                   // return _instance;
                }
                
            }
    
    
     public static async Task<ScreenManager> ProcessDataAsync()
            {
                var file = await StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appx:///Content/Load/ScreenManager.xml"));
                var str = await FileIO.ReadTextAsync(file);
           	    XmlManager<ScreenManager> xml = new XmlManager<ScreenManager>();
               _instance = xml.Load(str );
                return _instance;
               
            }
           

    and a couple of more ways but none of them worked... it usually gets stuck in the task.Result or task.Wait()..the app freezes in the first splash screen, pretty weird..

    im still waiting for help..

    thanks in advance,

    Romulo Romero


    Monday, March 3, 2014 1:20 AM
  • Got my solution here :

    http://stackoverflow.com/questions/9343594/how-to-call-asynchronous-method-from-synchronous-method-in-c

    i hope this helps someone else!

    Kind regards,

    Romulo Romero

    • Marked as answer by MrDebugging Monday, March 3, 2014 4:03 PM
    Monday, March 3, 2014 4:02 PM