locked
simple await not awaiting async RRS feed

  • Question

  • Boy am I missing something fundamental.  Shouldn't this entire method not return until it is actually done reading and writing?   I await this function for each of my objects.  However, the threads are colliding or something because I get a big nasty framework crash (further below)

    Caller:

    public async Task<bool> CreateBaseContent()
    {
       ...
       success = await SerializeMe.Serialize(ml);
       success = await SerializeMe.Serialize(tl);
       ...
    }

            public static async Task<bool> Serialize(object instance)
            {
                try
                {
                    DataContractSerializer ser = new DataContractSerializer(instance.GetType());
                    StorageFile file = await ApplicationData.Current.LocalFolder.CreateFileAsync(instance.GetType().FullName, CreationCollisionOption.ReplaceExisting);
                    // get the IRandomAccessStream from the file
                    using (IRandomAccessStream writeStream = await file.OpenAsync(FileAccessMode.ReadWrite))
                    {
                        //// convert to IO.Stream
                        System.IO.Stream stream = writeStream.AsStreamForWrite();
                        System.Xml.XmlWriterSettings settings = new System.Xml.XmlWriterSettings();
                        settings.Async = true;
                        using (System.Xml.XmlWriter writer = System.Xml.XmlWriter.Create(stream, settings))
                        {
                            ser.WriteObject(writer, instance);
                            await writer.FlushAsync();
                            
                        }
                    }
                }
                catch (Exception exception)
                {
                    // System.Diagnostics.Debug.WriteLine(exception.ToString());
                    return false;
                }
                return true;
            }

    Where the debugger kicks in:

    #if DEBUG && !DISABLE_XAML_GENERATED_BREAK_ON_UNHANDLED_EXCEPTION
                UnhandledException += (sender, e) =>
                {
                    if (global::System.Diagnostics.Debugger.IsAttached) global::System.Diagnostics.Debugger.Break();
                };
    #endif

    Saturday, April 27, 2013 12:28 AM

Answers

  • Problem lies here 

     async private void Page_Loaded_1

    This being void cannot be awaited (so caller is not awaiting for this event Page_Loaded_1) and somewhere this is causing all the issues. 

    If we are doing something like this we need to make sure resources are available before using them by listening to completed event or some other mechanism.


    Thanks,
    Sachin

    My Samples |Personal Website




    • Edited by Sachin S Saturday, April 27, 2013 5:52 PM
    • Marked as answer by Jesse Jiang Monday, May 6, 2013 5:30 AM
    Saturday, April 27, 2013 5:48 PM
  • Page_Loaded_1 is not being awaited so it is fine.
    It is only using await keyword.

    This kind of exception usually happens when something is wrong in XAML or data binding might causing this.
    There are also other cases that can cause this but XAML errors are most common.

    Try to use this:

    async private void Page_Loaded_1(object sender, RoutedEventArgs e)
            {
                try {
                    bool success = await InitContent();
                }
                catch (Exception e)
                {
                }
            }

    See if you get the same error and if not, see e for more information about why did this happened.
    • Edited by Jernej Kavka Sunday, April 28, 2013 9:20 PM
    • Marked as answer by Jesse Jiang Monday, May 6, 2013 5:30 AM
    Sunday, April 28, 2013 9:19 PM
  • Hi,

    On other hand, if you just want to serialize data, you can try DataContractSerializer class, add the know types.
    http://msdn.microsoft.com/en-us/library/system.runtime.serialization.datacontractserializer.aspx

    Best regards,
    Jesse


    Jesse Jiang
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.


    • Edited by Jesse Jiang Wednesday, May 1, 2013 4:20 AM
    • Proposed as answer by Jesse Jiang Friday, May 3, 2013 2:16 AM
    • Marked as answer by Jesse Jiang Monday, May 6, 2013 5:29 AM
    Wednesday, May 1, 2013 3:03 AM

All replies


  • Are you awaiting CreateBaseContent() ?

    http://lunarfrog.com/blog/

    Saturday, April 27, 2013 1:42 AM
  • Thanks for replying.  Yes I am awaiting it like this:

            async private void Page_Loaded_1(object sender, RoutedEventArgs e)
            {
                bool success = await InitContent();
            }

            async Task<Boolean> InitContent()
            {
                Statics.Content = new Content();
                return  await Statics.Content.InitializeContent();
            }


    async public Task<bool> InitializeContent() { return await InitializeContentAsync(); }


      public async Task<bool> InitializeContentAsync()
            {
                bool success = await  CreateBaseContent();
                
                object o;
                o = await SerializeMe.Deserialze(typeof(MonsterList));
                this.MonsterList = (MonsterList)o;
                o = await SerializeMe.Deserialze(typeof(TowerList));
                this.TowerList = (TowerList)o;
                o = await SerializeMe.Deserialze(typeof(WaveList));
                this.WaveList = (WaveList)o;
                return true;
            }


    Saturday, April 27, 2013 5:43 PM
  • Problem lies here 

     async private void Page_Loaded_1

    This being void cannot be awaited (so caller is not awaiting for this event Page_Loaded_1) and somewhere this is causing all the issues. 

    If we are doing something like this we need to make sure resources are available before using them by listening to completed event or some other mechanism.


    Thanks,
    Sachin

    My Samples |Personal Website




    • Edited by Sachin S Saturday, April 27, 2013 5:52 PM
    • Marked as answer by Jesse Jiang Monday, May 6, 2013 5:30 AM
    Saturday, April 27, 2013 5:48 PM
  • Page_Loaded_1 is not being awaited so it is fine.
    It is only using await keyword.

    This kind of exception usually happens when something is wrong in XAML or data binding might causing this.
    There are also other cases that can cause this but XAML errors are most common.

    Try to use this:

    async private void Page_Loaded_1(object sender, RoutedEventArgs e)
            {
                try {
                    bool success = await InitContent();
                }
                catch (Exception e)
                {
                }
            }

    See if you get the same error and if not, see e for more information about why did this happened.
    • Edited by Jernej Kavka Sunday, April 28, 2013 9:20 PM
    • Marked as answer by Jesse Jiang Monday, May 6, 2013 5:30 AM
    Sunday, April 28, 2013 9:19 PM
  • Hi,

    On other hand, if you just want to serialize data, you can try DataContractSerializer class, add the know types.
    http://msdn.microsoft.com/en-us/library/system.runtime.serialization.datacontractserializer.aspx

    Best regards,
    Jesse


    Jesse Jiang
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.


    • Edited by Jesse Jiang Wednesday, May 1, 2013 4:20 AM
    • Proposed as answer by Jesse Jiang Friday, May 3, 2013 2:16 AM
    • Marked as answer by Jesse Jiang Monday, May 6, 2013 5:29 AM
    Wednesday, May 1, 2013 3:03 AM
  • Wednesday, May 1, 2013 3:08 AM

  • Thanks for correcting my mistake.

    Best regards,
    Jesse


    Jesse Jiang
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Thursday, May 2, 2013 2:53 AM