Re: cross-thread UI updating?



On 24 May 2007 14:55:28 -0700, spacemarine@xxxxxxxxxxxxxx wrote:

'this is from MSDN article. but wont compile!?
statusLabel.BeginInvoke(New System.EventHandler(UpdateUI),
eventArgs)

I'm not very familiar with VB .NET but I think that you'd need to have this
instead:
statusLabel.BeginInvoke(New System.EventHandler(AddressOf UpdateUI),
eventArgs)

(note the AddressOf which is not required - and in fact not legal - in C#
but required AFAIK in VB .NET).

A couple of additional points:

- in the Load event handler of your form, you are calling
MethodInvoker.BeginInvoke() to call the method which is going to retrieve
data from your Web Service in a worker thread instead of calling it
directly from the UI thread:

Dim mi As MethodInvoker = New MethodInvoker(AddressOf FillMyData)
mi.BeginInvoke(Nothing, Nothing)

Accessing a web service in a worker thread instead of the UI thread is a
good idea as web service calls can block for sometimes a very long time
which means that they could freeze the UI if made from the UI thread.

However, when you call MethodInvoker.BeginInvoke(), what happens is that
the method that you passed as a parameter to BeginInvoke() is executed in a
thread from the system ThreadPool. The system ThreadPool only contains a
limited number of thread (25 per processor by default) and is used by the
..NET Framework itself to perform async operations. If all the ThreadPool
threads are already being used to perform some operations, any other
pending operation placed in the ThreadPool by yourself or the .NET
Framework is going to be queued until one of the ThreadPool's thread
becomes free again. It is therefore crucial that any operation performed in
a thread from the ThreadPool takes as little time as possible in order to
avoid starving the ThreadPool.

In your particular case, the method that you want executed in a ThreadPool
thread (FillMyData) calls a Web Service method which can take a very long
time to execute (seconds or even minutes). It's typically the kind of
operation that should not be executed in a thread from the ThreadPool.
Instead, use the asynchronouse methods that are provided by your Web
Service proxy class that has been generated by Visual Studio when you added
a Web Reference in your project (these methods are called BeginXXX - for
example if your web service has a method called GetData, then the proxy
class should contain a method called BeginGetData which allows you to call
GetData asynchronously without having to rely on the system ThreadPool).

- When you are calling Control.BeginInvoke() in your code, you are using
the EventHandler delegate to pass the address of the function you want
executed in the UI thread. This is perfectly fine in your particular case.

However, it is generally not a very good idea to use the EventHandler
delegate when calling Control.Invoke or Control.BeginInvoke as, in this
case, EventHandler will ignore the values that you passed for the 'sender'
and 'e' parameter and always use the control on which Invoke or BeginInvoke
has been called as the sender and always use EventArgs.Empty as the 'e'
parameter.

This is actually documented in the Control.Invoke() documentation but I've
been badly bitten by this weird behaviour a little while ago. I've spent a
long time trying to figure out why the 'sender' parameter was set to some
random value instead of being set to whatever I has set it. I eventually
figured out that EventHandler did its own thing completely ignoring the
parameters that were passed to it so I got rid of it and used custom
delegates which do not modify their parameters behind my back.
.



Relevant Pages

  • RE: Asynchronous Web Service Method Failure
    ... this is an ASP page calling the ... >that pops up if the proxy can't reconcile the web method parameters. ... I cannot return a .pdf document object from a web service method ... >> On the server side, it's not a background thread any more than any other ...
    (microsoft.public.dotnet.framework.aspnet.webservices)
  • Re: Trigger Timeout Loop Issue
    ... We're calling a third-party web service, ... The code can't be called from an external procedure (AFAIK) ... databases, or multiple schemas within one database. ...
    (comp.databases.oracle.server)
  • RE: OutOfMemoryException: Webservice, DataSet and file transport
    ... Calling the web service from a desktop pc gives no problems. ... achieve with the web method is updating the software. ... >> I keep the dataset in memory. ...
    (microsoft.public.dotnet.framework.compactframework)
  • Re: The underlying connection was closed
    ... I have written a web service that calls another web service. ... calling is third party, but written in .net. ... I am calling the 3rd party ws over https. ... No changes on the server or the source code. ...
    (microsoft.public.dotnet.framework.aspnet.webservices)
  • Upgrading an Application from XMLA 1.0 to 1.1
    ... I've got a small XMLA web service designed to run on XMLA1.0 but i've ... I generated the MsXMLAnalysis class ... No Overload Method for 'Execute' takes '2' arguments. ... I have the following Execute method in my code: ...
    (microsoft.public.data.xmlanalysis)