threads confussion

Eduardo Gomez 2,541 Reputation points
2021-09-20T22:44:25.103+00:00

I have a WPF application, that makes textboxes dynamically, the thins is that if I put a very large number, it will freeze, so I think I need another thread, but how can Igetitbavck to the UI

    public ObservableCollection<SfTextBoxExt> TextBoxesCollection { get; set; }

        public int? Value { get; set; }

        public ICommand CreateCommand { get; set; }

        public ICommand CreateFoldersCommand { get; set; }

        public ICommand SaveCommand { get; set; }


        public MainWindowVM() {

            SaveCommand = new Command(SaveAction);

            TextBoxesCollection = new ObservableCollection<SfTextBoxExt>();

            CreateCommand = new Command(CreateAction);

            CreateFoldersCommand = new Command(CreateFolderAction);
        }

        private void SaveAction() {
            if (TextBoxesCollection.Count > 0) {
                SaveFileDialog fileDialog = new SaveFileDialog();
                if (fileDialog.ShowDialog() == true) {
                    File.WriteAllLines(fileDialog.FileName, (System.Collections.Generic.IEnumerable<string>)TextBoxesCollection);
                }
            }

        }

        private void CreateFolderAction() {

            var dirrName = "Assets";

            var workingDirectory = Environment.CurrentDirectory;

            // This will get the current PROJECT directory
            var projectDirectories = Directory.GetParent(workingDirectory).Parent.Parent.GetDirectories(dirrName)
                .ToList();

            var path = Path.GetFullPath(projectDirectories.First().FullName);

            foreach (var item in TextBoxesCollection) {
                if (!Directory.Exists($"{path}\\{item.Text}")) {
                    Directory.CreateDirectory($"{path}\\{item.Text}");
                }
            }
        }


        private void CreateAction() {

            for (int i = 0; i < Value; i++) {
                TextBoxesCollection.Add(new SfTextBoxExt() {
                    VerticalAlignment = VerticalAlignment.Center,
                    Margin = new Thickness(0, 10, 80, 0),
                    Watermark = "Folder name"
                });
            }
        }
    }
}
Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,362 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Hui Liu-MSFT 17,226 Reputation points Microsoft Vendor
    2021-09-29T09:49:11.917+00:00

    As far as I know, with the exception of certain scheduler objects, we cannot perform this operation in a non-UI thread. We can convert some values between UI threads and non-UI threads, not objects. (Quoted from here)

    You can create elements in a background thread, but you can't do much to them. They belong to a background thread, and trying to attach them to a window created by the main thread will cause an error. Such as the question and the problem.

    The solution is to avoid creating a large number of elements at once. If possible, you could try to split the work of creating 3000 elements into smaller blocks (for example, create 10 elements at a time) and use the lower priority Dispatcher.BeginInvoke to run these blocks one by one.


    If the response is helpful, please click "Accept Answer" and upvote it.
     Note: Please follow the steps in our [documentation][5] to enable e-mail notifications if you want to receive the related email notification for this thread. 

    [5]: https://learn.microsoft.com/en-us/answers/articles/67444/email-notifications.html

    0 comments No comments