-
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
-
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 ).