Attaching a thread stops execution without exception

There are 2 threads in my application - the main thread of the user interface and another thread.

The application collects a list of file names in a separate stream. Then I want this list to appear in my GUI. The program works as desired until the thread terminates. This leads to the following error

"The calling thread must be STA, because many UI components require this."

The following code is executed, and, as you can see, it takes 2 action parameters - one for AddCurrentFile and one called Complete. During time, duplication.GetDuplicateList()he calls 2 methods.

        this._duplicatesThread = new Thread(() =>
        {
            try
            {
                duplication.GetDuplicateList(new Action<string>(AddCurrentFile), new Action<List<Duplicate>>(Complete));
                CreateParent();
            }
            catch (Exception ex)
            {
                string s = ex.ToString();
                throw;
            }
        });

AddCurrentFile provides a value for my property and through updates to my INotifyProperty GUI:

private void Complete(List<Duplicate> duplicateList)
{
   this._duplicateList = duplicateList;
}

, CreateParent(), UserControl - The calling thread must be STA, because many UI components require this.. UserControl. ,

    private void CreateParent()
    {
        ObservableCollection<DuplicateControl> parent = new ObservableCollection<DuplicateControl>();

        foreach (var duplicate in this._duplicateList)
        {
            DuplicateControl ic = new DuplicateControl();//This fails as soon as I enter the constructor. The DuplicateControl uses the UserControl as a base class
            parent.Add(ic);
        }
        this.ParentDuplicate = parent;
    }

, , CreateParent() this._duplicatesThread.Join;

, (, , / ), , , , , UI, ! , this._duplicatesThread.Join;

, , , , ( , , ).

, ( ), ? , , NewThread.FinishedExecuting += DoThisThing()

+3
2

ViewModel (UI) WPF . ViewModel , WPF INotifyPropertyChanged.PropertyChanged ( , .NET 4.5, AFAIK). , .

ViewModel , , .. ViewModel . Dispatcher.Invoke () Dispatcher.InvokeAsync (, ).

Progress<T> . /:

wpf, ?

+3
Thread thread = new Thread(MethodWhichCallesTheConstructor);
        thread.SetApartmentState(ApartmentState.STA); //Set the thread to STA
thread.Start(); 
thread.Join(); //Wait for the thread to end
0

All Articles