private whenSomethingHappens()
{
MethodAsync methodAsync = new MethodAsync(this.Method);
IAsyncResult ar = methodAsync.BeginInvoke(param, new
AsyncCallback(MethodComplete), null);
}
private delegate void MethodAsyncAsync(paramType param);
private void Method(paramType param)
{
// whatever happens here...
}
private void MethodComplete(IAsyncResult call)
{
// whatever happens here
}
In other words, when some event happens, we create a reference to the
delegate which makes the call for us on a new thread which will call a given
method back when it finishes.
The idea here is that this is called from the user interface and this method
goes on to carry about its work but the user interface is still responsive.
When I do this, the time it takes the "Method" above to execute is
*significantly* longer than if it is simply called directly on the same
thread as the UI/main program.
I understand and expect there to be a certain performance difference but if
the UI is really essentially doing nothing, why does it take *so long* for
the Method to execute asynchronously? Is this normal? Am I doing something
within the Method that is causing the performance to degrade like
referencing objects in another thread (the calling thread)?
Any ideas?
- Chris