none
Assembly.Load executed in Thread, blocks ui RRS feed

  • Question

  • 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>); }
            }

    Friday, October 7, 2016 12:11 PM

All replies