Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / March 2008

Tip: Looking for answers? Try searching our database.

c#3.0 and delegate question

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
CSharper - 25 Mar 2008 20:10 GMT
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.
Peter Duniho - 25 Mar 2008 20:18 GMT
> [...]
> 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
CSharper - 25 Mar 2008 20:23 GMT
On Mar 25, 2:18 pm, "Peter Duniho" <NpOeStPe...@nnowslpianmk.com>
wrote:
> > [...]
> > In 3.0 I can not find the InvokeRequired. Is there something changed
[quoted text clipped - 18 lines]
>
> 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.
CSharper - 25 Mar 2008 20:25 GMT
> On Mar 25, 2:18 pm, "Peter Duniho" <NpOeStPe...@nnowslpianmk.com>
> wrote:
[quoted text clipped - 32 lines]
>
> - Show quoted text -

Sorry, yes, it is System.Window.Window instead of Form.
Peter Duniho - 25 Mar 2008 20:49 GMT
> 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?

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 is a  
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
CSharper - 25 Mar 2008 20:54 GMT
On Mar 25, 2:49 pm, "Peter Duniho" <NpOeStPe...@nnowslpianmk.com>
wrote:
> > 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
[quoted text clipped - 32 lines]
>
> 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.
CSharper - 25 Mar 2008 21:15 GMT
On Mar 25, 2:49 pm, "Peter Duniho" <NpOeStPe...@nnowslpianmk.com>
wrote:
> > 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
[quoted text clipped - 32 lines]
>
> Pete

Excellent, it make perfect sense now. Thanks.
Gilles Kohl [MVP] - 25 Mar 2008 20:30 GMT
>I have a callback method where I am going to update GUI with a status.
>In C#2.0 I can do the following
[quoted text clipped - 10 lines]
>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.
CSharper - 25 Mar 2008 20:39 GMT
> >I have a callback method where I am going to update GUI with a status.
> >In C#2.0 I can do the following
[quoted text clipped - 21 lines]
>    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.
Jon Skeet [C# MVP] - 25 Mar 2008 21:19 GMT
<snip>

> 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.

Signature

Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk


Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.