|
-
June 12th, 2011, 08:26 PM
#1
[RESOLVED] Using SendOrPostCallback
Hello,
I'm hoping someone can help me with this. I'm new to C#, and can't figure out how to correct this.
I get the error: No overload for 'Lbl39Text' matches delegate 'System.Threading.SendOrPostCallback'
I'm using VS2010.
See the attached offending code (in red)!
Thanks!
Code:
private void Lbl39Text(string NewText)
{
this.Label39.Text = NewText;
this.Refresh();
}
Code:
if (this.Label39.InvokeRequired) {
// It's on a different thread, so use Invoke.
SendOrPostCallback d1 = new SendOrPostCallback(Lbl39Text);
this.Invoke(d1, new object[] { NewText });
} else {
// It's on the same thread, no need for Invoke.
this.Label39.Text = NewText;
Refresh();
}
Last edited by KKW; June 13th, 2011 at 09:14 AM.
-
June 17th, 2011, 07:05 AM
#2
Re: Using SendOrPostCallback
The problem is that the signature of the Lbl39Text method doesn't match the one that the SendOrPostCallback delegate expects.
See the documentation here: the delegate accepts a method that returns void and takes a parameter of type object.
public delegate void SendOrPostCallback(object state);
Remarks
[...]
Your method must have the signature shown here.
This means that Lbl39Text can't accept a string, but an object (but since everything implicitly derives from object, you can still pass a string - or anything else). However, you'll have to cast it back to string within the method.
string val = (string)NewText; // or: string val = NewText as string;
this.Label39.Text = val;
// a refresh is not required
You might also wanna know that Control-derived classes, Label included, have their own Invoke() method - see here.
Last edited by TheGreatCthulhu; June 17th, 2011 at 07:10 AM.
-
August 31st, 2011, 09:23 PM
#3
Re: Using SendOrPostCallback
Sorry for my absence, I've been ill.
However my condition afforded me extra time to learn about delegates and threads etc.
So it wasn't all bad!
The issue has been resolved by someone else while I was out.
Thanks for the help!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|