How to switch between streams in c # .net to update the GUI?

  • 0
    I have a thread (not the main thread) that is working with treeView, so this is how I call the main thread to update the treeView

    I read about delegates, invoke ... I can't figure it out without an example yet.

    Something like this:

    Working in a background thread ....
    We switched to the main stream
    treeView.items.add (item);
    Working in a background thread ....
    C# Anonymous, Nov 29, 2019

  • 4 Answers
  • 0
    Another option:
    this.Invoke(new Action(() =>
    // работа с treeView

  • 0
    There may be several options. I remember it well like this:
    this.Invoke((MethodInvoker) delegate { 

    I don't see any point in InvokeRequired. In specifying a specific control instead of this - too.
    Arabella Patrick

  • 0
    You can use SynchronizationContext

    // Инициализируете в UI потоке
    synchContext = AsyncOperationManager.SynchronizationContext;
    // Затем из потоков
    synchContext.Post(OnComplete, data);
    // Где OnComplete уже будет выполняться в контексте UI

  • 0
    Don't throw pissing slippers on))

    And apply only when you understand what you are doing
    Control.CheckForIllegalCrossThreadCalls = False
    Delilah Wilkins

Your Answer
To place the code, please use CodePen or similar tool. Thanks you!