Quantcast
Channel: Windows Presentation Foundation (WPF) forum
Viewing all articles
Browse latest Browse all 18858

Assembly.Load executed in Thread, blocks ui

$
0
0

Executing the folowing code with "new Thread(new ThreadStart(tstest)).Start();" Freezes the UI while the Assemblys are Loading

How can I prevent the freezing of the UI?

void tstest()
{
foreach(var item in GetAsembys("folder with a lot *.exe files"))
{
}
}

    public class AssemblyHolder
    {

        #region Fields

        public Assembly originAssembly;

        public String originPath;

        private FileVersionInfo m_FileVersion;

        private Version m_AssemblyVersion;

        #endregion Fields

        #region Properties

        public FileVersionInfo FileVersion { get { return m_FileVersion = m_FileVersion ?? GetFileVersion(); } set { m_FileVersion = value; } }
        public Version AssemblyVersion { get { return m_AssemblyVersion = m_AssemblyVersion ?? GetAssemblyVersion(); } set { m_AssemblyVersion = value; } }

        #endregion Properties

        #region Constructors

        public AssemblyHolder(Assembly _asm, String _originPath)
        {
            originAssembly = _asm;
            originPath = _originPath;
        }

        #endregion Constructors

        #region Methods

        public static implicit operator Assembly(AssemblyHolder d)
        {
            return d.originAssembly;
        }

        public FileVersionInfo GetFileVersion()
        {
            return FileVersionInfo.GetVersionInfo(originPath);
        }

        private Version GetAssemblyVersion()
        {
            return originAssembly.GetName().Version;
        }

        #endregion Methods
    }

   public static List<AssemblyHolder> GetAsembys(String _rootfolder, SearchOption _option = SearchOption.AllDirectories)
        {
            try
            {
                List<AssemblyHolder> asm = new List<AssemblyHolder>();
                var executables = Directory.GetFiles(_rootfolder, "*.exe", _option);

                foreach (var item in executables)
                    try
                    {
                        using (Stream stream = File.OpenRead(item))
                        {
                            byte[] rawAssembly = new byte[stream.Length];
                            stream.Read(rawAssembly, 0, (int)stream.Length);
                            asm.Add(new AssemblyHolder(Assembly.Load(rawAssembly), item));
                        }
                    }
                    catch { }

                return asm;
            }
            catch { return default(List<AssemblyHolder>); }
        }


Viewing all articles
Browse latest Browse all 18858

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>