December 2007
Intermediate to advanced
896 pages
19h 57m
English
You have determined that one or more delegates invoked synchronously within your application are taking a long time to execute. This delay is making the user interface less responsive to the user. The invocation of these delegates should be converted from synchronous to asynchronous mode.
A typical synchronous delegate type and supporting code that invokes the delegate are shown here:
public delegate void SyncDelegateTypeSimple();
public class TestSyncDelegateTypeSimple
{
public static void Method1()
{
Console.WriteLine("Invoked Method1");
}
}The code to use this delegate is:
public static void TestSimpleSyncDelegate()
{
SyncDelegateTypeSimple sdtsInstance = TestSyncDelegateTypeSimple.Method1;
sdtsInstance();
}This delegate can be called asynchronously on a thread obtained from the thread pool by modifying the code as follows:
public static void TestSimpleAsyncDelegate()
{
AsyncCallback callBack = new AsyncCallback(DelegateSimpleCallback); SyncDelegateTypeSimple sdtsInstance = TestSyncDelegateTypeSimple.Method1; IAsyncResult asyncResult = sdtsInstance.BeginInvoke(callBack, null); Console.WriteLine("WORKING..."); } // The callback that gets called when TestSyncDelegateTypeSimple.Method1 // is finished processing private static void DelegateSimpleCallback(IAsyncResult iResult) { AsyncResult result = (AsyncResult)iResult; SyncDelegateTypeSimple sdtsInstance = (SyncDelegateTypeSimple)result.AsyncDelegate; ...Read now
Unlock full access