Thread: Cross Threading Problems

  1. #1
    Registered User (TNT)'s Avatar
    Join Date
    Aug 2001
    Location
    UK
    Posts
    339

    Cross Threading Problems

    Hi,

    Im having problems trying to update my GUI controls from a new thread i have created, i get an exception about cross threading. I have looked around and found i need to use a delegate and invoke it from the new thread, but im having problems getting this to work. Heres what ive done:

    Main form:
    Code:
            public delegate void UpdateFDelegate(string item);
    
    
            public void UpdateList(string item)
            {
                
                if (this.FileView.InvokeRequired)
                {
                    UpdateFDelegate theDelegate = new UpdateFDelegate(this.UpdateList);
                    this.Invoke(theDelegate, new object[] { item });
                }
    
                else
                {
                    ListViewItem li = new ListViewItem();
                    li.Text = "test";
                    this.FileView.Items.Add(li);
                }
    
            }
    From inside my thread class: (which inherits from the main form class)
    Code:
    UpdateList("test");
    Now this does not throw an exception, but it dosnt work either, my debugger shows it just goes straight to the else block, i.e no invoking is done, but nothing is added to the listivew control, and ive tried adding other test code in there like changing a buttons text, but nothing on the GUI can update from it.

    Any ideas what im doing wrong?

    Thanks
    Jack
    TNT
    You Can Stop Me, But You Cant Stop Us All

  2. #2
    and the Hat of Clumsiness GanglyLamb's Avatar
    Join Date
    Oct 2002
    Location
    between photons and phonons
    Posts
    1,110
    When I was still very much into C# programming ( about a year ago ) I had similar problems. The problem was not my code, but actually Visual Studio.

    When working with threads I would get the same exception as you when I ran the application from within visual Studio, but as soon as I ran the app from outside visual studio everything would go just fine....

    Maybe this isn't the case with you, but its worth trying ( I remember I spend alot of time trying to find the bug in my code, that actually wasn't there ).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. all about cross compilation
    By munna_dude in forum Linux Programming
    Replies: 1
    Last Post: 08-27-2008, 04:15 AM
  2. Looking for light cross platform Threading library
    By umen242 in forum C++ Programming
    Replies: 7
    Last Post: 03-28-2008, 04:23 PM
  3. Minimax, URGENT :(
    By Dark-MX0Z in forum C++ Programming
    Replies: 6
    Last Post: 11-14-2007, 06:29 AM
  4. c++ threading
    By Anddos in forum C++ Programming
    Replies: 4
    Last Post: 12-28-2005, 03:29 PM
  5. Problem with threading
    By osal in forum Windows Programming
    Replies: 6
    Last Post: 07-21-2004, 12:41 PM