PDA

Click to See Complete Forum and Search --> : [resolved] [2.0] BackgroundWorker + Updating Form


supertotallyawesome
Jun 25th, 2006, 04:35 PM
I have a label on a form. I am starting another function in a backgroundworker. I want to update the label with text a few times per loop. There is a significant delay between each update, so I'm not worried about refreshing the label and hosing up the main thread.

I don't understand how delegates work. I know that I need to invoke something in order to get the label updated. But, I don't know the syntax or how to do it. I did this code below, but I don't think it's correct and I don't know how to actually call the function without getting a Cross-Thread exception. :(


delegate void dosomestuff(string value, string binary);
private void dosomestuffmethod(string value, string binary)
{
label1.Text = "Value: " + value + "\nBinary: " + binary;
}


Can someone point me to a tutorial or simple code snippet that will help? All the examples via google search just give me headaches. I know how to use a backgroundworker, I just need to know how to call something in a different thread.

Thanks

---

resolution:


delegate void del_DoSomeStuff(string value, string binary);
private void DoSomeStuff_Method(string value, string binary)
{
label1.Text = "Value: " + value + "\nBinary: " + binary;
}

private void LightItUp(int val) // stripped down version of thread function
{
label1.Invoke(new del_DoSomeStuff(DoSomeStuff_Method), new object[] { val.ToString(), DecToBin(val) });
}