Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. How can I access my Mainwindow-controls from a thread?

How can I access my Mainwindow-controls from a thread?

Scheduled Pinned Locked Moved C#
questionworkspace
4 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • E Offline
    E Offline
    Erik
    wrote on last edited by
    #1

    Hi, I am currently writing a gui-application, which does some processing in a thread. I pass a reference to my mainwindow to the thread, in order to make the thread set some text inside the mainwindow. However, when I try to access any control in my mainwindow from my thread, I get: "An unhandled exception of type 'System.InvalidOperationException' occurred in WindowsBase.dll" Simple question: what is the best way to access mainwindo-gui elements from a thread? This is what my code looks like:

    public MainWindow()
    {
    InitializeComponent();
    _ProcessHelperThread = new System.Threading.Thread(ProcessHelperThread);
    _ProcessHelperThread.Start();
    }

        void ProcessHelperThread()
        {
            Processes.Init(this); //this is a reference to my mainwindow
            //run the processes defined in Startup configuration
            Processes.Startup();
        }
    

    Inside the thread (i.e. in my Processes-object), my app crashes when I try something like this:

     public int Init(MainWindow window)
        {
            window.textBlock1.Text = Waittext; //Exception
        }
    

    What is a better way to do this?

    D 1 Reply Last reply
    0
    • E Erik

      Hi, I am currently writing a gui-application, which does some processing in a thread. I pass a reference to my mainwindow to the thread, in order to make the thread set some text inside the mainwindow. However, when I try to access any control in my mainwindow from my thread, I get: "An unhandled exception of type 'System.InvalidOperationException' occurred in WindowsBase.dll" Simple question: what is the best way to access mainwindo-gui elements from a thread? This is what my code looks like:

      public MainWindow()
      {
      InitializeComponent();
      _ProcessHelperThread = new System.Threading.Thread(ProcessHelperThread);
      _ProcessHelperThread.Start();
      }

          void ProcessHelperThread()
          {
              Processes.Init(this); //this is a reference to my mainwindow
              //run the processes defined in Startup configuration
              Processes.Startup();
          }
      

      Inside the thread (i.e. in my Processes-object), my app crashes when I try something like this:

       public int Init(MainWindow window)
          {
              window.textBlock1.Text = Waittext; //Exception
          }
      

      What is a better way to do this?

      D Offline
      D Offline
      David Ewen
      wrote on last edited by
      #2

      I would have expected a cross thread exception rather than the one you got but the correct way to update gui elements from another thread is the call Invoke on the control. This is for WinForms not WPF.

      public Form1()
      {
      InitializeComponent();

      Thread workerThread = new Thread(WorkerThread);
      workerThread.Start();
      

      }

      private void WorkerThread()
      {
      UpdateWindowText(this);
      }

      private void UpdateWindowText(Form1 form)
      {
      form.Invoke((Action) (() => form.Text = "test"));
      }

      D 1 Reply Last reply
      0
      • D David Ewen

        I would have expected a cross thread exception rather than the one you got but the correct way to update gui elements from another thread is the call Invoke on the control. This is for WinForms not WPF.

        public Form1()
        {
        InitializeComponent();

        Thread workerThread = new Thread(WorkerThread);
        workerThread.Start();
        

        }

        private void WorkerThread()
        {
        UpdateWindowText(this);
        }

        private void UpdateWindowText(Form1 form)
        {
        form.Invoke((Action) (() => form.Text = "test"));
        }

        D Offline
        D Offline
        David Ewen
        wrote on last edited by
        #3

        As a follow up this is how it is done in WPF.

        public MainWindow()
        {
        InitializeComponent();

        Thread workerThread = new Thread(WorkerThread);
        workerThread.Start();
        

        }

        private void WorkerThread()
        {
        UpdateWindowText(this);
        }

        private void UpdateWindowText(Window window)
        {
        window.Dispatcher.Invoke((Action) (() => window.Title = "test"));
        }

        E 1 Reply Last reply
        0
        • D David Ewen

          As a follow up this is how it is done in WPF.

          public MainWindow()
          {
          InitializeComponent();

          Thread workerThread = new Thread(WorkerThread);
          workerThread.Start();
          

          }

          private void WorkerThread()
          {
          UpdateWindowText(this);
          }

          private void UpdateWindowText(Window window)
          {
          window.Dispatcher.Invoke((Action) (() => window.Title = "test"));
          }

          E Offline
          E Offline
          Erik
          wrote on last edited by
          #4

          Thanks alot, this solution works perfectly!

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups