
Signature
HTH,
Kevin Spencer
Chicken Salad Surgeon
Microsoft MVP
Hi,
> The class containing the function can implement an Event, which the
> function can call whenever it wants to report something during execution.
> The client application can subscribe to the Event and handle it in any way
> desired.
An event is not the right solution, the event will be executed in the
calling thread, not in the main thread. That is why you need to use
Control.Invoke cause its assure you that the call will be executed in the
Control's creation thread.

Signature
Ignacio Machin
http://www.laceupsolutions.com
Mobile & warehouse Solutions.
roundcrisis - 11 Jan 2008 17:49 GMT
On Jan 11, 2:35 pm, "Ignacio Machin \( .NET/ C# MVP \)" <machin TA
laceupsolutions.com> wrote:
> Hi,
>
[quoted text clipped - 18 lines]
> Ignacio Machinhttp://www.laceupsolutions.com
> Mobile & warehouse Solutions.
i read somewhere that i should use begin invoke over invoke
does this make sense and if so why and where should i use end invoke?
thanks
Peter Duniho - 11 Jan 2008 18:18 GMT
> [...]
> i read somewhere that i should use begin invoke over invoke
> does this make sense and if so why
You can use either. Invoke() is useful if you need or want to wait on the
return value for the delegate being executed, or if you want to take
advantage of the inherent synchronization Invoke() provides.
BeginInvoke() is useful if you don't care about the result of the
invocation and want the invoking thread to continue with its processing.
But using BeginInvoke() you will have to ensure synchronization between
any data structures used both in the invoked method and the invoking
thread, if any.
> and where should i use end invoke?
For Control.BeginInvoke() _only_, the unofficial information is that
calling Control.EndInvoke() is not required. For all other examples of a
BeginXXX() method, you must call the corresponding EndXXX() method after
the asynchronous operation has completed.
Using Control.BeginInvoke(), you might still want to call
Control.EndInvoke() if you want to retrieve the return value from the
method that was invoked. In that case, you can call EndInvoke() whenever
you want, but be aware that if the invoked method hasn't completed yet,
EndInvoke() will wait and not return until the invoked method is done.
Pete
Peter Duniho - 11 Jan 2008 18:21 GMT
> An event is not the right solution, the event will be executed in the
> calling thread, not in the main thread.
Events and cross-thread issues are not mutually exclusive. I use events
for thread signaling on a regular basis and it works fine.
Of course, you do need to ensure that you use Invoke() or BeginInvoke()
when calling GUI method. If the event-raising code is in a
Control-derived class, this is easy enough to do all the time, but even if
not it's simple enough to do so from an event handler.
Pete