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. WPF
  4. WPF and Threading

WPF and Threading

Scheduled Pinned Locked Moved WPF
helpcsharpwpfquestion
3 Posts 3 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.
  • C Offline
    C Offline
    cdpace
    wrote on last edited by
    #1

    Hello everyone, I have the following peace of code in a WPF application.

    System.Threading.ThreadStart threadstartdelegate = delegate()
    {
    for (int i = 0; i < 100; i++)
    {
    System.Threading.Thread.Sleep(1000);
    OnlineClientListBoxItem item = new OnlineClientListBoxItem();
    item.SetUserDetails(Name: "Client " + i.ToString(), Userid: i);

                    lstbOnlineClients.Dispatcher.Invoke(new Action<OnlineClientListBoxItem>((x) =>
                        {
                            lstbOnlineClients.Items.Add(x);
                        }), System.Windows.Threading.DispatcherPriority.Normal, item);
                }
            };
            System.Threading.Thread mainThread = new System.Threading.Thread(threadstartdelegate);
            mainThread.SetApartmentState(System.Threading.ApartmentState.STA);
            mainThread.Start();
    

    and at this part lstbOnlineClients.Items.Add(x); it is giving me the following error "The calling thread cannot access this object because a different thread owns it." can anyone help me because this is the method in which you can invoce a control's method from another thread. Any ideas? Thank you in advance

    A 1 Reply Last reply
    0
    • C cdpace

      Hello everyone, I have the following peace of code in a WPF application.

      System.Threading.ThreadStart threadstartdelegate = delegate()
      {
      for (int i = 0; i < 100; i++)
      {
      System.Threading.Thread.Sleep(1000);
      OnlineClientListBoxItem item = new OnlineClientListBoxItem();
      item.SetUserDetails(Name: "Client " + i.ToString(), Userid: i);

                      lstbOnlineClients.Dispatcher.Invoke(new Action<OnlineClientListBoxItem>((x) =>
                          {
                              lstbOnlineClients.Items.Add(x);
                          }), System.Windows.Threading.DispatcherPriority.Normal, item);
                  }
              };
              System.Threading.Thread mainThread = new System.Threading.Thread(threadstartdelegate);
              mainThread.SetApartmentState(System.Threading.ApartmentState.STA);
              mainThread.Start();
      

      and at this part lstbOnlineClients.Items.Add(x); it is giving me the following error "The calling thread cannot access this object because a different thread owns it." can anyone help me because this is the method in which you can invoce a control's method from another thread. Any ideas? Thank you in advance

      A Offline
      A Offline
      Abhishek Sur
      wrote on last edited by
      #2

      This is the most general issue. You can access UI objects only from UI thread. In WPF the UI thread is pointed using Dispatcher object. So you can replace the code

      lstbOnlineClients.Items.Add(x);

      with this.Dispatcher.Invoke(DispatcherPriority.Normal, new System.Windows.Forms.MethodInvoker(delegate() { lstbOnlineClients.Items.Add(x); })); Dispatcher will invoke the code in the UI thread. I hope this will help you. :rose:

      Abhishek Sur **Don't forget to click "Good Answer" if you like this Solution.


      Visit My Website-->**

      www.abhisheksur.com

      A 1 Reply Last reply
      0
      • A Abhishek Sur

        This is the most general issue. You can access UI objects only from UI thread. In WPF the UI thread is pointed using Dispatcher object. So you can replace the code

        lstbOnlineClients.Items.Add(x);

        with this.Dispatcher.Invoke(DispatcherPriority.Normal, new System.Windows.Forms.MethodInvoker(delegate() { lstbOnlineClients.Items.Add(x); })); Dispatcher will invoke the code in the UI thread. I hope this will help you. :rose:

        Abhishek Sur **Don't forget to click "Good Answer" if you like this Solution.


        Visit My Website-->**

        www.abhisheksur.com

        A Offline
        A Offline
        AspDotNetDev
        wrote on last edited by
        #3

        The OP already uses Dispather.Invoke.

        [Forum Guidelines]

        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