Async windows programming

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Three quick (I hope) questions regarding async programming in a windows
form:

1) When I perform a BeginInvoke on a delegate in my own thread (actually the
main GUI thread) in the midst of a message event handler (say OnMouseUp),
does the implementation basically work the same as the BeginInvoke from
another thread, i.e., a message is posted, the event handler completes
execution, then when the message is dequeued, the target method is invoked?

2) Is there a complete list of how synchronous paint calls can be generated?
I know that doing an Invalidate() simply queues a paint message, but adding
an Update() causes a synchronous paint message. If my handler for the async
event absolutely needs to know that OnPaint(0 is not entered while it is
doing it's work, what else could it do incorrectly, (besides calling
Update()) that could cause a synchronous OnPaint()

3) Are any other calls to handlers made synchronously (like mouse moves or
mouse up/down, etc)?
 
Mark Stega said:
Three quick (I hope) questions regarding async programming in a windows
form:

1) When I perform a BeginInvoke on a delegate in my own thread (actually the
main GUI thread) in the midst of a message event handler (say OnMouseUp),
does the implementation basically work the same as the BeginInvoke from
another thread, i.e., a message is posted, the event handler completes
execution, then when the message is dequeued, the target method is invoked?

You need to be a bit more precise here - are you calling BeginInvoke
*on* a delegate (in which case the threadpool is used) or are you
calling Control.BeginInvoke and passing in a delegate, in which case
the UI thread will be used.
 
Calling BeginInvoke *on* a delegate (in which case the threadpool is used)

Using "FireAndForget" of:

public class AsyncHelper
{
/// <summary>
/// Delegate to wrap another delegate and its arguments
/// </summary>
delegate void DelegateWrapper(Delegate d, object[] args);

/// <summary>
/// An instance of DelegateWrapper which calls InvokeWrappedDelegate,
/// which in turn calls the DynamicInvoke method of the wrapped
/// delegate.
/// </summary>
static DelegateWrapper wrapperInstance = new
DelegateWrapper(InvokeWrappedDelegate);

/// <summary>
/// Callback used to call <code>EndInvoke</code> on the asynchronously
/// invoked DelegateWrapper.
/// </summary>
static AsyncCallback callback = new AsyncCallback(EndWrapperInvoke);

/// <summary>
/// Executes the specified delegate with the specified arguments
/// asynchronously on a thread pool thread.
/// </summary>
public static void FireAndForget(Delegate d, params object[] args)
{
// Invoke the wrapper asynchronously, which will then
// execute the wrapped delegate synchronously (in the
// thread pool thread)
wrapperInstance.BeginInvoke(d, args, callback, null);
}

/// <summary>
/// Invokes the wrapped delegate synchronously
/// </summary>
static void InvokeWrappedDelegate(Delegate d, object[] args)
{
d.DynamicInvoke(args);
}
}
 
Back
Top