Search code examples
c#serializationwindows-phone-8sharpserializer

TargetInvocationException when serializeing with SharpSerializer


I am trying to serialize with sharpSerializer. But I get TargetInvocationException and InnerException as UnauthorizedAccessException: "Invalid cross-thread access".

Here my code for serializing:

public static async Task Save<T>(this T obj, string file)
{
    await Task.Run(() =>
    {
        IsolatedStorageFile storage = IsolatedStorageFile.GetUserStoreForApplication();
        IsolatedStorageFileStream stream = null;

        try
        {
            stream = storage.CreateFile(file);
            var serializer = new SharpSerializer();
            serializer.Serialize(obj, stream); // Exception occurs here
        }
        catch (Exception)
        {
        }
        finally
        {
            if (stream != null)
            {
                stream.Close();
                stream.Dispose();
            }
        }
    });
}

I'm serializing in App.xaml.cs

private void Application_Closing(object sender, ClosingEventArgs e)
{
    hs_layout.Save("layout.xml");
}

Type of hs_layout

public static List<Homescreen> hs_layout = new List<Homescreen>();

Homescreen class

public class Homescreen 
    {
        public List<UIElement>[ , ] Main { get; set; }
        public List<UIElement>[ ] Dock { get; set; }
        
        public Homescreen()
        {
            Main = new List<UIElement>[5, 4];
            for (int i = 0; i < 5; i++)
            {
                for (int j = 0; j < 4; j++)
                {
                    Main[i, j] = new List<UIElement>();
                }
            }

             Dock = new List<UIElement>[5];                    
            for (int j = 0; j < 5; j++)
            {
                 Dock[j] = new List<UIElement>();
            }
        }
    }

Solution

  • This worked for me

    public static async Task Save<T>(this T obj, string file)
            {
                Deployment.Current.Dispatcher.BeginInvoke((Action)(() =>
                        {
                    IsolatedStorageFile storage = IsolatedStorageFile.GetUserStoreForApplication();
                    IsolatedStorageFileStream stream = null;
    
                    try
                    {
                        stream = storage.CreateFile(file);
                        var serializer = new SharpSerializer();
    
                        serializer.Serialize(obj, stream);
    
    
                        //serializer.Serialize(obj, stream);
                    }
                    catch (Exception)
                    {
                    }
                    finally
                    {
                        if (stream != null)
                        {
                            stream.Close();
                            stream.Dispose();
                        }
                    }
                }));
            }
    
            public static async Task<T> Load<T>(string file)
            {
    
                IsolatedStorageFile storage = IsolatedStorageFile.GetUserStoreForApplication();
                T obj = Activator.CreateInstance<T>();
    
                if (storage.FileExists(file))
                {
                    IsolatedStorageFileStream stream = null;
                    try
                    {
                        stream = storage.OpenFile(file, FileMode.Open);
                        var serializer = new SharpSerializer();
    
                        obj = (T)serializer.Deserialize(stream);
                    }
                    catch
                    {
                    }
                    finally
                    {
                        if (stream != null)
                        {
                            stream.Close();
                            stream.Dispose();
                        }
                    }
                    return obj;
                }
                await obj.Save(file);
                return obj;
            }
        }