c#3.0 and delegate question

  • Thread starter Thread starter CSharper
  • Start date Start date
C

CSharper

I have a callback method where I am going to update GUI with a status.
In C#2.0 I can do the following

if (this.InvokeRequired)
{
//Do invoke
}
else
{
//display message
}

In 3.0 I can not find the InvokeRequired. Is there something changed
in it?

Thanks.
 
[...]
In 3.0 I can not find the InvokeRequired. Is there something changed
in it?

Nothing's changed with respect to that.  If you can't find the  
InvokeRequired property, you're using the wrong class somehow.  Maybe you  
accidently got a WPF class (System.Windows.Controls) instead of a Forms  
class (System.Windows.Forms)?

That said, I'll take this opportunity to share my opinion that the use of  
the InvokeRequired property is unnecessary anyway.  :)

Just always invoke some code that you want to execute on the main GUI  
thread.  Obviously this means you can't invoke the method that does the  
invoking, but whether you use an anonymous method or call a different  
helper method, that's simple enough to accomplish.

I really don't like the template MSDN offers for this situation, because  
you have a single method that effectively does two completely different  
things depending on which thread it's executing on.

Pete

Hi Pete,

Thanks and it is a WPF project but my main class is derived from
Window class. Based on what I understood, you are saying not to check
for invoke required instead call the delegate with a method which will
update the GUI always? Is it right?

Thanks.
 
[...]
In 3.0 I can not find the InvokeRequired. Is there something changed
in it?
Nothing's changed with respect to that.  If you can't find the  
InvokeRequired property, you're using the wrong class somehow.  Maybe you  
accidently got a WPF class (System.Windows.Controls) instead of a Forms  
class (System.Windows.Forms)?
That said, I'll take this opportunity to share my opinion that the use of  
the InvokeRequired property is unnecessary anyway.  :)
Just always invoke some code that you want to execute on the main GUI  
thread.  Obviously this means you can't invoke the method that does the  
invoking, but whether you use an anonymous method or call a different  
helper method, that's simple enough to accomplish.
I really don't like the template MSDN offers for this situation, because 
you have a single method that effectively does two completely different  
things depending on which thread it's executing on.

Hi Pete,

Thanks and it is a WPF project but my main class is derived from
Window class.  Based on what I understood, you are saying not to check
for invoke required instead call the delegate with a method which will
update the GUI always? Is it right?

Thanks.- Hide quoted text -

- Show quoted text -

Sorry, yes, it is System.Window.Window instead of Form.
 
I have a callback method where I am going to update GUI with a status.
In C#2.0 I can do the following

if (this.InvokeRequired)
{
//Do invoke
}
else
{
//display message
}

In 3.0 I can not find the InvokeRequired. Is there something changed
in it?

No, not last time I checked. Control.InvokeRequired is "supported in: 3.5, 3.0
SP1, 3.0, 2.0 SP1, 2.0, 1.1, 1.0" according to MSDN.

Did anything else change in your code at the same time? Of what type is your
class that has the callback (i.o.W., "this"), is it derived from Control?

Maybe provide a bit more context ...

Regards,
Gilles.
 
No, not last time I checked. Control.InvokeRequired is "supported in: 3.5,3.0
SP1, 3.0, 2.0 SP1, 2.0, 1.1, 1.0" according to MSDN.

Did anything else change in your code at the same time? Of what type is your
class that has the callback (i.o.W., "this"), is it derived from Control?

Maybe provide a bit more context ...

   Regards,
   Gilles.

This is a WPF project and I am trying to update a TextBlock and change
the text in it. Yes, it is not a form control, it is a Window.Window
control.
Thanks.
 
Well, don't call the delegate directly.  Do use Invoke() (or  
BeginInvoke()).  But yes, don't bother checking whether invoking is  
required.  If it's not, it's only slightly more overhead to do the invoke  
anyway, and presumably this code is in a place where practically all the  
time you'd need to invoke anyway.

Of course, all this assumes you're using a System.Windows.Forms control,  
where calling Invoke() would be required.  Seeing as you've said this isa  
Window, not a Form, and that you're using WPF, the above advice may not be 
applicable at all.

Note: I did a quick search, and found this article:http://msdn2.microsoft.com/en-us/library/ms741870.aspx

If I read it correctly, it appears that WPF shares the same cross-thread  
issues that exist for a Forms-based application, but address them in a  
different way.  Instead of there being an Invoke() method on the  
individual controls, there's a Dispatcher object that you use to call  
Invoke().  You can get this Dispatcher from the WPF object using the  
Dispatcher property (inherited from the DispatcherObject class) of your  
WPF controls (and maybe from the Window as well...I didn't look).

I note that in the Dispatcher class, there's no direct way to find out  
whether the Dispatcher is for the current thread or not.  It has a Thread  
property, and you could compare that to the current Thread instance.  But  
that's a bit awkward.  It appears they expect you to just call Invoke()  
without checking to see whether doing so is required.

In other words, it looks like the folks who designed WPF agree with me on  
the InvokeRequired property question.  :)

Pete

Thank you very much and I will read the information and yes, it make
sense to call the control without double function coding in a single
method.
 
Well, don't call the delegate directly.  Do use Invoke() (or  
BeginInvoke()).  But yes, don't bother checking whether invoking is  
required.  If it's not, it's only slightly more overhead to do the invoke  
anyway, and presumably this code is in a place where practically all the  
time you'd need to invoke anyway.

Of course, all this assumes you're using a System.Windows.Forms control,  
where calling Invoke() would be required.  Seeing as you've said this isa  
Window, not a Form, and that you're using WPF, the above advice may not be 
applicable at all.

Note: I did a quick search, and found this article:http://msdn2.microsoft.com/en-us/library/ms741870.aspx

If I read it correctly, it appears that WPF shares the same cross-thread  
issues that exist for a Forms-based application, but address them in a  
different way.  Instead of there being an Invoke() method on the  
individual controls, there's a Dispatcher object that you use to call  
Invoke().  You can get this Dispatcher from the WPF object using the  
Dispatcher property (inherited from the DispatcherObject class) of your  
WPF controls (and maybe from the Window as well...I didn't look).

I note that in the Dispatcher class, there's no direct way to find out  
whether the Dispatcher is for the current thread or not.  It has a Thread  
property, and you could compare that to the current Thread instance.  But  
that's a bit awkward.  It appears they expect you to just call Invoke()  
without checking to see whether doing so is required.

In other words, it looks like the folks who designed WPF agree with me on  
the InvokeRequired property question.  :)

Pete

Excellent, it make perfect sense now. Thanks.
 
This is a WPF project and I am trying to update a TextBlock and change
the text in it. Yes, it is not a form control, it is a Window.Window
control.

WPF doesn't use Invoke directly on the controls. You use the Dispatcher
property to get a Dispatcher, and then call Invoke on that.
 
Back
Top