Re: When is Invoke() Really Required?

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance

From: Sijin Joseph (sijindotnet_at_hotmail.com)
Date: 04/08/04


Date: Thu, 8 Apr 2004 17:08:19 +0530

Hi Kenny,

In my experience if your method does not touch any UI related operations
then it is safe to call that method. You see when you call a method it runs
in the context of the calling thread, now if the calling thread does not
have the message pump related infrastructure( For details refer to
Programming applications for windows by Jeffery Ritcher) then posting or
generating any messages from that thread won't work. Since ultimately even
.Net controls work using messaging the results will be unexpected. That any
method that might generate a message is a candidate for calling through
Invoke()

-- 
-Sijin Joseph
http://weblogs.asp.net/sjoseph
"Kenny Carruthers" <ken@kennyc.com> wrote in message
news:O3cKpJPHEHA.2720@TK2MSFTNGP11.phx.gbl...
>     The documentation for InvokeRequired states that:
>
> ---------------
> There are four methods on a control that are safe to call from any thread:
> Invoke, BeginInvoke, EndInvoke and CreateGraphics. For all other method
> calls, you should use one of these invoke methods when calling from a
> different thread.
> ---------------
>
>     Does this apply for absolutely every method or property in a control
or
> just those methods and properties that actually touch the UI? For example,
> if I have a custom control derived from UserControl, can I call methods on
> that custom control that don't touch any user interface elements from
> another thread? What about for properties.
>
>     Clearly I can't call things like SetText, ForeColor, etc... But it
would
> seem save to call something a method or property that just does some
> internal calculation or am I wrong?
>
>     On a related note, it would seem that from the description above, that
I
> can't call Invalidate() inside a method if that method was called from a
> worker thread. But doesn't Invalidate() just post a WM_PAINT message to
the
> UI thread? Why do I have to go through Invoke() when calling Invalidate()?
>
> Thanks in advance for any help.
> Kenny
>
>


Relevant Pages

  • Re: InvokeRequired? (multithreading)
    ... public delegate void functionNameCallback; ... If you're calling the function from the main thread, and at that point checking if "InvokeRequired" will indeed be false. ... Invoke isn't required at that point. ... I always put my background work in a separate thread, then use the dispatcher to talk back to the control. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Correct way to call a Delegate?
    ... > The correct way to call back into the UI thread is by using Invoke(). ... > under the control of the calling thread. ... > Using Invoke causes the delegate's method code to run under the control ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Accessing UI components on a background thread.
    ... The only methods on a class that derives from Control that are safe to ... invoke from any thread are Invoke, BeginInvoke, EndInvoke and CreateGraphics ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Changing ProgressBar in a seperate Thread
    ... (this is the only thread-safe property of a control) ... Invoke a delegate initialized with your changeProgressBar method ... instead of calling it directly. ...
    (microsoft.public.dotnet.languages.csharp)
  • Invoking calls on a control.
    ... I have had cases where I am calling a method on a control within a thread, ... which may produce intermittent problems. ... invoke to invoke the method on the control's thread. ...
    (microsoft.public.dotnet.framework.windowsforms)