Click here to Skip to main content
15,890,741 members
Home / Discussions / C#
   

C#

 
GeneralRe: Thread locking Pin
Meysam Mahfouzi11-Jun-04 17:21
Meysam Mahfouzi11-Jun-04 17:21 
GeneralRe: Thread locking Pin
Xiangyang Liu 刘向阳12-Jun-04 3:01
Xiangyang Liu 刘向阳12-Jun-04 3:01 
GeneralRe: Thread locking Pin
link_7913-Jun-04 9:57
link_7913-Jun-04 9:57 
GeneralRe: Thread locking Pin
Meysam Mahfouzi11-Jun-04 17:13
Meysam Mahfouzi11-Jun-04 17:13 
GeneralPlaceHolder, TreeView, UserControl Pin
caheo11-Jun-04 9:22
caheo11-Jun-04 9:22 
GeneralRe: PlaceHolder, TreeView, UserControl Pin
Heath Stewart11-Jun-04 10:40
protectorHeath Stewart11-Jun-04 10:40 
Questioninefficient to create new delegate every time? Pin
vista2711-Jun-04 8:10
vista2711-Jun-04 8:10 
AnswerRe: inefficient to create new delegate every time? Pin
Heath Stewart11-Jun-04 8:48
protectorHeath Stewart11-Jun-04 8:48 
You don't need to create the delegate each time. If you're invoking the same method over and over again, create the delegate once and keep a reference to it. It's merely a managed method pointer.

You can create a delegate that references both static and instance methods, but you have to include either the class name or the object reference with the method, just as if you were going to call the method without using a delegate (and the same rules for methods defined on the executing class are the same as well).

So, lets say you want to set the text for a TextBox from another thread and you expect to do it quite a bit. Do something like this:
private delegate void SetValueDelegate(object obj, object value,
  object[] index);
private SetValueDelegate _setValueDelegate;
public void SafeSetText(TextBox tb, string value)
{
  if (tb == null) throw new ArgumentNullException("tb");
  if (tb.InvokeRequired)
  {
    if (_setValueDelegate== null)
    {
      PropertyInfo pi = tb.GetType().GetProperty("Text");
      _setValueDelegate= new SetValueDelegate(pi.SetValue);
    }
    tb.Invoke(_setValueDelegate, new object[] {tb, value, null});
  }
  else tb.Text = value;
}
This is just a sample, of course, but notice how I create a delegate that references an instance method using pi.SetValue. Even though the delegate isn't referencing a method on the control (that's much easier; with getting/setting properties you have to use reflection or the component model), the delegate is still invoked in the control's thread in which it was created.

 

Microsoft MVP, Visual C#
My Articles
QuestionRichTextBox cannot handle huge amount of color-coded, varied font text? Pin
vista2711-Jun-04 7:47
vista2711-Jun-04 7:47 
AnswerRe: RichTextBox cannot handle huge amount of color-coded, varied font text? Pin
Aaron Eldreth11-Jun-04 8:32
Aaron Eldreth11-Jun-04 8:32 
GeneralRe: RichTextBox cannot handle huge amount of color-coded, varied font text? Pin
vista2711-Jun-04 9:16
vista2711-Jun-04 9:16 
GeneralRe: RichTextBox cannot handle huge amount of color-coded, varied font text? Pin
Aaron Eldreth11-Jun-04 12:18
Aaron Eldreth11-Jun-04 12:18 
GeneralUpdate to DB Failure Pin
MrJJKoolJ11-Jun-04 7:44
MrJJKoolJ11-Jun-04 7:44 
GeneralRe: Update to DB Failure Pin
Colin Angus Mackay11-Jun-04 8:10
Colin Angus Mackay11-Jun-04 8:10 
GeneralRe: Update to DB Failure Pin
MrJJKoolJ11-Jun-04 8:33
MrJJKoolJ11-Jun-04 8:33 
GeneralRe: Update to DB Failure Pin
Heath Stewart11-Jun-04 8:49
protectorHeath Stewart11-Jun-04 8:49 
GeneralRe: Update to DB Failure Pin
MrJJKoolJ11-Jun-04 9:11
MrJJKoolJ11-Jun-04 9:11 
GeneralRe: Update to DB Failure Pin
Heath Stewart11-Jun-04 10:38
protectorHeath Stewart11-Jun-04 10:38 
GeneralRe: Update to DB Failure Pin
MrJJKoolJ11-Jun-04 11:26
MrJJKoolJ11-Jun-04 11:26 
GeneralRe: Update to DB Failure Pin
Heath Stewart11-Jun-04 11:52
protectorHeath Stewart11-Jun-04 11:52 
GeneralRe: Update to DB Failure Pin
MrJJKoolJ11-Jun-04 12:19
MrJJKoolJ11-Jun-04 12:19 
GeneralImport a comma delimited text file to a access database Pin
kornstyle11-Jun-04 7:31
kornstyle11-Jun-04 7:31 
GeneralRe: Import a comma delimited text file to a access database Pin
Heath Stewart11-Jun-04 8:30
protectorHeath Stewart11-Jun-04 8:30 
GeneralDataGridTextBoxColumn won't word wrap Pin
holt11-Jun-04 6:05
holt11-Jun-04 6:05 
GeneralRe: DataGridTextBoxColumn won't word wrap Pin
Heath Stewart11-Jun-04 6:27
protectorHeath Stewart11-Jun-04 6:27 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.