Hi there,
Iam trying to make an app that has about 14 different threads which have to access the GUI and update some of the labels. Now if i do that, after a little whil it kinda hangs, I think its becaus the gui has been accesed by two or more threads at the same time. So i tried to solve this writing this code:
The problem is when i trie to run this it will still hang becaus there are two different lock objects which both try to get to the gui. I also tried this:
Here the whole program hangs, i think its because there is one lock object thats being used for two different codes, i also tried this:
And this also makes the app hang =(. So my question is how to solve this? or do i rly have to use a background worker on updating labels/texboxes in the gui?
Heres an example of the time being updated in the gui:
Thx in advance =D!
Iam trying to make an app that has about 14 different threads which have to access the GUI and update some of the labels. Now if i do that, after a little whil it kinda hangs, I think its becaus the gui has been accesed by two or more threads at the same time. So i tried to solve this writing this code:
Code:
private void SetText(string text, Label item)
{
if (item.InvokeRequired)
{
lock (myLockObject)
{
SetTextCallback d = new SetTextCallback(SetText);
this.Invoke(d, new object[] { text, item });
}
}
else
{
lock (myLockObject2)
{
item.Text = text;
}
}
}
Code:
private void SetText(string text, Label item)
{
if (item.InvokeRequired)
{
lock (myLockObject)
{
SetTextCallback d = new SetTextCallback(SetText);
this.Invoke(d, new object[] { text, item });
}
}
else
{
lock (myLockObject)
{
item.Text = text;
}
}
}
Code:
private void SetText(string text, Label item)
{
lock (myLockObject)
{
if (item.InvokeRequired)
{
SetTextCallback d = new SetTextCallback(SetText);
this.Invoke(d, new object[] { text, item });
}
else
{
item.Text = text;
}
}
}
Heres an example of the time being updated in the gui:
Code:
private void tijd()
{
while (stoppen == 0)
{
System.Threading.Thread.Sleep(500);
SetText(DateTime.Now.ToString(),lbl_tijd);
}
}
Comment