Wait thread question

I have a UserControl with a tree on it. It uses multithreading to add nodes to it. I have a function called Expand that I need to execute after filtering is complete, and since I'm new to the multi-threaded process, I'm not sure how to do this. Here is my code:

class MyClass : UserControl
{
    private Thread nThread;
    private bool searchLoadCompleted = false;

    private void Filter()
    {
        ClearTree();
        this.nThread = new Thread(new ParameterizedThreadStart(AddFilteredResultsToTree));
        this.nThread.IsBackground = true;
        this.nThread.Start(someParameter);
    }

    private void AddFilteredResultsToTree(int someParameter)
    {
        myTree.Invoke(new MethodInvoker( ()=> this.searchLoadCompleted = false ));
        myTree.Invoke(new MethodInvoker( ()=> AppendNode(......) ));
        myTree.Invoke(new MethodInvoker( ()=> this.searchLoadCompleted = true ));
    }   

    private void Expand()
    {
    }
}

I tried adding nThread.Join()in Expand(), but it stuck endlessly. What should I do?

+3
source share
2 answers

If this is a single-processor version:

ClearTree();
AddFilteredResultsToTree(someparameter);
Expand();

, . , , ( - ) . , , Invoke, AddFilteredResultsToTree .

Expand AddFilteredResult .

( , ) Async Pattern ( ), AsyncCallback.

+2

Calling Invoke GUI , .

+1

Source: https://habr.com/ru/post/1723228/


All Articles