locked
IVisualCollection<T>.AddNew() should not be called from UI Thread RRS feed

  • Question

  • Where am I going wrong with this:

    For Each Dir As String In Directory.EnumerateDirectories(Path)
                        Me.TempDocs.AddNew()
                        With Me.TempDocs.SelectedItem
                            Dim d As New DirectoryInfo(Dir)
                            .Created = d.CreationTime
                            .LastModified = d.LastWriteTime
                            .Name = d.Name
                            .Size = 0
                        End With
    
                    Next

    Is there an issue with accessing the file system in the same thread?

    This is a 2 tier SL application.


    Sunday, June 23, 2013 12:30 AM

Answers

  • If you need to access the file system I would use a WCF RIA Service:

    A Visual Studio LightSwitch Picture File Manager

       public FileDomainService()
        {
            _LocalFiles = new List<FileRecord>();
            string strFileDirectory = GetFileDirectory();
            EnsureDirectory(new System.IO.DirectoryInfo(strFileDirectory));
            // Add all directories at this directory.
            DirectoryInfo objDirectoryInfo = new DirectoryInfo(strFileDirectory);
            string FolderName = objDirectoryInfo.Name;
            // Get the files in the directory
            foreach (var fi in objDirectoryInfo.EnumerateFiles().OrderBy(x => x.Name))
            {
                FileRecord objFileRecord = new FileRecord();
                objFileRecord.FileName = fi.Name;
                // Load Image
                string strPath = string.Format(@"{0}\{1}", strFileDirectory, fi.Name);
                FileStream sourceFile = new FileStream(strPath, FileMode.Open);
                long FileSize;
                FileSize = sourceFile.Length;
                byte[] getContent = new byte[(int)FileSize];
                sourceFile.Read(getContent, 0, (int)sourceFile.Length);
                sourceFile.Close();
                objFileRecord.FileImage = getContent; 
                // Add file to the collection
                _LocalFiles.Add(objFileRecord);
            }
        }


    The Visual Studio LightSwitch Marketplace

    http://LightSwitchHelpWebsite.com

    • Marked as answer by Angie Xu Monday, July 8, 2013 1:48 AM
    Sunday, June 23, 2013 1:45 AM

All replies

  • If you need to access the file system I would use a WCF RIA Service:

    A Visual Studio LightSwitch Picture File Manager

       public FileDomainService()
        {
            _LocalFiles = new List<FileRecord>();
            string strFileDirectory = GetFileDirectory();
            EnsureDirectory(new System.IO.DirectoryInfo(strFileDirectory));
            // Add all directories at this directory.
            DirectoryInfo objDirectoryInfo = new DirectoryInfo(strFileDirectory);
            string FolderName = objDirectoryInfo.Name;
            // Get the files in the directory
            foreach (var fi in objDirectoryInfo.EnumerateFiles().OrderBy(x => x.Name))
            {
                FileRecord objFileRecord = new FileRecord();
                objFileRecord.FileName = fi.Name;
                // Load Image
                string strPath = string.Format(@"{0}\{1}", strFileDirectory, fi.Name);
                FileStream sourceFile = new FileStream(strPath, FileMode.Open);
                long FileSize;
                FileSize = sourceFile.Length;
                byte[] getContent = new byte[(int)FileSize];
                sourceFile.Read(getContent, 0, (int)sourceFile.Length);
                sourceFile.Close();
                objFileRecord.FileImage = getContent; 
                // Add file to the collection
                _LocalFiles.Add(objFileRecord);
            }
        }


    The Visual Studio LightSwitch Marketplace

    http://LightSwitchHelpWebsite.com

    • Marked as answer by Angie Xu Monday, July 8, 2013 1:48 AM
    Sunday, June 23, 2013 1:45 AM
  • Sunday, June 23, 2013 2:24 AM